Welcome Guest, Not a member yet? Register   Sign In
Bug in routes with path in controller
#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 [font=Monaco, Consolas, Courier, monospace]Backend\Users[/font]
Reply


Messages In This Thread
Bug in routes with path in controller - by acy29 - 11-22-2019, 05:44 AM
RE: Bug in routes with path in controller - by danipperez - 05-06-2021, 09:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB