Welcome Guest, Not a member yet? Register   Sign In
Bug in routes with path in controller
#1

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

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...uting.html
Sercan YANBULOGLU
SEO links REDACTED, per forum policy
Reply
#3

(This post was last modified: 11-25-2019, 04:42 AM by acy29.)

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!
Reply
#4

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

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?
Reply
#6

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

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

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

/ is for folder names or URL: blog/en/some-article
\ is for namespaces: App\Controllers\Blog::index
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#10

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']);
}); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB