Welcome Guest, Not a member yet? Register   Sign In
Controllers under subfolder
#1

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
Reply
#2

(This post was last modified: 06-30-2016, 11:57 PM by sv3tli0.)


  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 .
Best VPS Hosting : Digital Ocean
Reply
#3

(This post was last modified: 07-01-2016, 06:13 AM by InsiteFX. Edit Reason: Spelling error )

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.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(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.
Reply
#5

(This post was last modified: 07-01-2016, 02:13 PM by arma7x.)

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.
Keep calm.
Reply
#6

(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.
Reply
#7

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,
Reply
#8

(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.
Keep calm.
Reply
#9

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.
Best VPS Hosting : Digital Ocean
Reply
#10

(This post was last modified: 07-02-2016, 02:40 AM by arma7x.)

(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).
Keep calm.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB