CodeIgniter Forums

Full Version: Controllers under subfolder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hi,

I have a controller under a sub-folder (Controllers\ajax\Test.php) but when I try to access from the browser (/ajax/test/testmethod) I get an error message "Controller or its method is not found."

Please note if I do a class map in Config\Autoload.php like this:

 $classmap = [
      "Test" =>  APPPATH . "Controllers/ajax/Test.php"
 ];

then it works.  However, If I namespace my controller (namespace Ajax) then the controllers does not get loaded and get the same error message.

Is there any workaround or should I do it in another way.

Thanks in advance and best regards,
Fabian

  1. Remove this class map which you have set.
  2. Your real namespace should be something as 
    Code:
    namespace App\Controllers\Ajax;
  3. Make the first letter of ajax capital at all places - this is the standard applied to CI.
  4. At App/Config/Routes check if you have set 
    Code:
    $routes->setDefaultNamespace('App\Controllers');
    $routes->setAutoRoute(true);
    - at least with the current CI 4 version at the repository they are set .
I played around with this the other night for awhile, this is what I came up with.

Folder Structure:

Controllers
-- -- Admin
-- -- -- -- AdminController.php
-- -- Base
-- -- -- -- BaseController.php
-- -- Frontend
-- -- -- -- FrontController.php
-- -- Home.php

BaseController.php

PHP Code:
<?php namespace App\Controllers\Base;

class 
BaseController extends \CodeIgniter\Controller
{
    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class BaseController. 

AdminController.php

PHP Code:
<?php namespace App\Controllers\Admin;

use 
App\Controllers\Base\BaseController;

class 
AdminController extends BaseController
{
    public function 
__construct (...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class AdminController. 

FrontController.php

PHP Code:
<?php namespace App\Controllers\Frontend;

use 
App\Controllers\Base\BaseController;

class 
FrontController extends BaseController
{
    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class FrontController. 

Home.php

PHP Code:
<?php namespace App\Controllers;

// use
//use App\Controllers\Frontend\AdminController;
use App\Controllers\Frontend\FrontController

// or
// class Home extends AdminController

class Home extends FrontController
{
    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct(...$params)
    {
        
parent::__construct(...$params);
    }

    public function 
index()
    {
        echo 
view('welcome_message');
    }

    
//------------------------------------------------------------------------

  // End of Class Home

/**
 * ---------------------------------------------------------------------------
 * Filename: Home.php
 * Location: ./application/Controllers/Home.php
 * ---------------------------------------------------------------------------
 */ 

Of course I'am not even sure if this is the correct way but it does work for me.

This should get you started on doing sub-folders, I hope it helps.
(07-01-2016, 04:08 AM)InsiteFX Wrote: [ -> ]I played around with this the other night for awhile, this is what I came up with.

Folder Structure:

Controllers
-- -- Admin
-- -- -- -- AdminController.php
-- -- Base
-- -- -- -- BaseController.php
-- -- Frontend
-- -- -- -- FrontController.php
-- -- Home.php

BaseController.php

PHP Code:
<?php namespace App\Controllers\Base;

class 
BaseController extends \CodeIgniter\Controller
{
    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class BaseController. 

AdminController.php

PHP Code:
<?php namespace App\Controllers\Admin;

use 
App\Controllers\Base\BaseController;

class 
AdminController extends BaseController
{
    public function 
__construct (...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class AdminController. 

FrontController.php

PHP Code:
<?php namespace App\Controllers\Frontend;

use 
App\Controllers\Base\BaseController;

class 
FrontController extends BaseController
{
    public function 
__construct(...$params)
    {
        
parent::__construct(...$params);
    }

  // End of Class FrontController. 

Home.php

PHP Code:
<?php namespace App\Controllers;

// use
//use App\Controllers\Frontend\AdminController;
use App\Controllers\Frontend\FrontController

// or
// class Home extends AdminController

class Home extends FrontController
{
    
/**
     * Class variables - public, private, protected and static.
     * -----------------------------------------------------------------------
     */


    /**
     * __construct ()
     * -----------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct(...$params)
    {
        
parent::__construct(...$params);
    }

    public function 
index()
    {
        echo 
view('welcome_message');
    }

    
//------------------------------------------------------------------------

  // End of Class Home

/**
 * ---------------------------------------------------------------------------
 * Filename: Home.php
 * Location: ./application/Controllers/Home.php
 * ---------------------------------------------------------------------------
 */ 

Of course I'am not even sure if this is the correct way but it does work for me.

This should get you started on doing sub-folders, I hope it helps.
replace 
namespace App\Controllers\Base;
with
namespace Base;
It's work for me.
Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller.
(07-01-2016, 02:12 PM)arma7x Wrote: [ -> ]Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller.
i'm not sure, put My_Controller in libraries is good idea. Base controller is not a library.
Hi All,

thanks for your feedback and suggestions.

I have tried all but still the same problem.  I downloaded the latest CI 4 from repository.

My environment is Windows.

I will keep trying.  If any additional tip, would be highly appreciated.

Thanks and regards,
(07-01-2016, 09:20 PM)titounnes Wrote: [ -> ]
(07-01-2016, 02:12 PM)arma7x Wrote: [ -> ]Why not put "MY_Controller" inside libraries folder and add namespace App\Libraries in "MY_Controller" then extend it to \CodeIgniter\Controller. Now to use it, extends all controller in controller folder to \App\Libraries\MY_Controller.
i'm not sure, put My_Controller in libraries is good idea. Base controller is not a library.

You can put your own "MY_Controller" in any folder/create new one inside application folder as long as you give namespace to where it located.
Remember that MY_ classes existed in the old CI versions because there were no namespaces.. Now in CI 4 you are able simply to create App/Controllers/Controller and to use it from your other controllers..
The era of MY_ logic is over.
(07-01-2016, 10:07 PM)fromberg Wrote: [ -> ]Hi All,

thanks for your feedback and suggestions.

I have tried all but still the same problem.  I downloaded the latest CI 4 from repository.

My environment is Windows.

I will keep trying.  If any additional tip, would be highly appreciated.

Thanks and regards,

This is controller file inside sub-folder Ajax in Controller folder;
PHP Code:
<?php namespace App\Controllers\Ajax;

class 
Test extends \CodeIgniter\Controller
{

    public function 
index()
    {
                echo 
'hi Im test in ajax sub folder';
    }

    
//--------------------------------------------------------------------



Edit your route file, /application/Config/Routes.php by adding:
PHP Code:
$routes->add('ajax/test''Test::index', ['namespace' => 'App\Controllers\Ajax']); 

It's works for me. Using CI4, if you namespace the controller, you have to set $routes->setDefaultNamespace('App\Controllers'); and define $routes->add() for each controller(Having 404NotFound problem if I’m did not do this).
Pages: 1 2 3 4