CodeIgniter Forums
Bug in routes with path in controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Bug in routes with path in controller (/showthread.php?tid=74899)

Pages: 1 2


Bug in routes with path in controller - acy29 - 11-22-2019

Hi, Routes with pass parameters such as the following:
$routes->get('/movie/test/(:any)', 'dashboard/MovieController::test/$1');

They only work if the controller (for example MovieController) is in the root:
$routes->get('/movie/test/(:any)', 'MovieController::test/$1');



In the first case ($routes->get('/movie/test/(:any)', 'dashboard/MovieController::test/$1');) the error it gives is the following

404 - File Not Found
Controller or its method is not found: \App\Controllers\dashboard::index


It does not recognize the controller according to the error it throws; But if I remove the dasboard from the configuration like:

$routes->add('movie/test/(:any)', 'MovieController::test/$1');

its working


RE: Bug in routes with path in controller - ProfessionalCoder - 11-25-2019

You can use only

$routes->add('journals', 'App\Blogs'); if dashboard is files and it have controller you must use \ .Its run default index function.
$routes->add('blog/joe', 'Blogs::users'); if dashboard is controller you must use scope + function

you can see https://codeigniter4.github.io/userguide/incoming/routing.html


RE: Bug in routes with path in controller - acy29 - 11-25-2019

dashboard is a folder:

-Controllers (folder)
  -dashboard (folder)
    -MovieController (class PHP - controller)

I want to put the controllers of the administrative module inside a folder (dashboard) and in another folder my Rest Api (rest folder)

thank you!


RE: Bug in routes with path in controller - davis.lasis - 11-25-2019

(11-25-2019, 04:41 AM)acy29 Wrote: dashboard is a folder:

-Controllers (folder)
  -dashboard (folder)
    -MovieController (class PHP - controller)

I want to put the controllers of the administrative module inside a folder (dashboard) and in another folder my Rest Api (rest folder)

thank you!

Dashboard folder must be with first upper case letter D. Try that, cause at least that was the case few month ago


RE: Bug in routes with path in controller - midhunlalkc - 11-30-2019

Am also experiencing the same issue with routes with parameters
PHP Code:
    $routes->group('usergroups', ['filter' => 'ajax'], function($routes){
        
$routes->add('(:num)/edit''Backend/Users::editGroup/$1', ['as' => 'edit_group']);
    }); 

This route is not working. Am getting 404 error. Controller method is not found: index

I have controller in:  App/Controllers/Backend/Users.php

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

use 
App\Controllers\Backend;
use 
App\Models\Backend\UserModel;

class 
Users extends Backend
{
    protected $userModel;

    public function __construct(){
    
parent::__construct();
    
$this->userModel = new UserModel();
    }

    public function editGroup($groupId)    
    {
    
$data['group'] = $this->userModel->getGroupDetails($groupId);
    echo 
view'backend/users/edit_group'$data);
    }


how can i fix this issue?


RE: Bug in routes with path in controller - burgoyn1 - 12-04-2019

(11-30-2019, 05:41 AM)midhunlalkc Wrote: Am also experiencing the same issue with routes with parameters
PHP Code:
    $routes->group('usergroups', ['filter' => 'ajax'], function($routes){
        
$routes->add('(:num)/edit''Backend/Users::editGroup/$1', ['as' => 'edit_group']);
    }); 

This route is not working. Am getting 404 error. Controller method is not found: index

I have controller in:  App/Controllers/Backend/Users.php

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

use 
App\Controllers\Backend;
use 
App\Models\Backend\UserModel;

class 
Users extends Backend
{
    protected $userModel;

    public function __construct(){
    
parent::__construct();
    
$this->userModel = new UserModel();
    }

    public function editGroup($groupId)    
    {
    
$data['group'] = $this->userModel->getGroupDetails($groupId);
    echo 
view'backend/users/edit_group'$data);
    }


how can i fix this issue?

It is because codeigniter is expecting all of your controllers to be in one folder, App/Controllers. If you try to reference it out of that, then it says it can't find it and throws a 404 error.

The solution is really simple, just tell it where the new namespace is:

$routes->add('(:num)/edit', 'Users::editGroup/$1', ['as' => 'edit_group', 'namespace' => 'App\Controllers\Backend']);

https://codeigniter4.github.io/userguide/incoming/routing.html#assigning-namespace

Hope that helps you solve your issue.


RE: Bug in routes with path in controller - danipperez - 05-06-2021

(12-04-2019, 06:24 AM)burgoyn1 Wrote:
(11-30-2019, 05:41 AM)midhunlalkc Wrote: Am also experiencing the same issue with routes with parameters
PHP Code:
    $routes->group('usergroups', ['filter' => 'ajax'], function($routes){
        
$routes->add('(:num)/edit''Backend/Users::editGroup/$1', ['as' => 'edit_group']);
    }); 

This route is not working. Am getting 404 error. Controller method is not found: index

I have controller in:  App/Controllers/Backend/Users.php

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

use 
App\Controllers\Backend;
use 
App\Models\Backend\UserModel;

class 
Users extends Backend
{
    protected $userModel;

    public function __construct(){
    
parent::__construct();
    
$this->userModel = new UserModel();
    }

    public function editGroup($groupId)    
    {
    
$data['group'] = $this->userModel->getGroupDetails($groupId);
    echo 
view'backend/users/edit_group'$data);
    }


how can i fix this issue?

It is because codeigniter is expecting all of your controllers to be in one folder, App/Controllers. If you try to reference it out of that, then it says it can't find it and throws a 404 error.

The solution is really simple, just tell it where the new namespace is:

$routes->add('(:num)/edit', 'Users::editGroup/$1', ['as' => 'edit_group', 'namespace' => 'App\Controllers\Backend']);

https://codeigniter4.github.io/userguide/incoming/routing.html#assigning-namespace

Hope that helps you solve your issue.

TRY : Backend/Users to Backend\Users


RE: Bug in routes with path in controller - paulkd - 05-13-2021

TRY : Backend/Users to Backend\Users
[/quote]

I also have this issue and worked out that changing / to \ works.

The line of code that doesn't play well with the "controllers in folders" is in vendor\codeigniter4\framework\system\Router\Router.php  (:660)

Code:
list($controller, $method) = array_pad(explode('::', $segments[0]), 2, null);

So...

This works
Code:
$routes->get('about', 'Front/Main::about');
This doesn't
Code:
$routes->get('products/(children|men|women)', 'Front/Main::getProducts/$1');
This does
Code:
$routes->get('products/(children|men|women)', 'Front\Main::getProducts/$1');

I'm quite happy to use / but I would like to know if I may get bitten further down the road?  Cool


RE: Bug in routes with path in controller - includebeer - 05-13-2021

/ is for folder names or URL: blog/en/some-article
\ is for namespaces: App\Controllers\Blog::index


RE: Bug in routes with path in controller - InsiteFX - 05-13-2021

You both need to add the namespaces.

PHP Code:
$routes->get('/movie/test/(:any)''MovieController::test/$1', ['namespace' => 'Dashboard']);

$routes->group('usergroups', ['filter' => 'ajax'], ['namespace' => 'Backend'], function($routes){
        $routes->add('(:num)/edit''Users::editGroup/$1', ['as' => 'edit_group']);
});