CodeIgniter Forums
Myth/Auth - Routing problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: Myth/Auth - Routing problem (/thread-76073.html)

Pages: 1 2


Myth/Auth - Routing problem - BilltheCat - 04-12-2020

I can't see what I'm doing wrong here, as everything I've read seems to say this SHOULD work.  I'm filtering routes to the User controller, and the controller index method does NOT work, while the profile method works fine.  I'm lost... Confused

Very basic implementation here:

App\Controllers\User.php
PHP Code:
<?php namespace App\Controllers;

class 
User extends Auth
{
    public function 
index()
    {
           echo "Test index";
    }
        
        
public function profile()
    {
           echo "Test profile";
    }



App\Config\Routes.php
PHP Code:
//Restricted to logged in users
$routes->group('user', ['filter' => 'role:Users'], function($routes) {
    $routes->get('user''User::index'); // Does NOT work
    $routes->get('profile''User::profile');  //Works Correctly
}); 



RE: Myth/Auth - Routing problem - includebeer - 04-12-2020

The first route would give domain.com/user/user
Is this the URL your are typing? What doesn’t work? Do you get a 404 or something else?

Maybe this would make more sense?
PHP Code:
$routes->group('user', ['filter' => 'role:Users'], function($routes) {
    $routes->get('/''User::index'); // domain.com/user
    $routes->get('profile''User::profile');  // domain.com/user/profile
}); 



RE: Myth/Auth - Routing problem - BilltheCat - 04-12-2020

I have the URLs as 
domain.com/user/index
And
domain.com/user/profile

Doesn'ft your method with '/' point the routing to the home controller? 
The problem is that the filter doesn't prevent guest users from visiting the User controller index.


RE: Myth/Auth - Routing problem - BilltheCat - 04-13-2020

Well, it works... I'm not sure why yet, but I guess I have some documentation to read up on.  Thanks for your help, @includebeer


RE: Myth/Auth - Routing problem - includebeer - 04-13-2020

No that’s not the URL these routes will point to because you are using a group.
The group function is there to not having to repeat the same things for all route.

PHP Code:
//Restricted to logged in users
$routes->group('user', ['filter' => 'role:Users'], function($routes) {
    $routes->get('user''User::index'); // Does NOT work
    $routes->get('profile''User::profile');  //Works Correctly
}); 

...is the same as:

PHP Code:
$routes->get('user/user''User::index', ['filter' => 'role:Users']); // Does NOT work
$routes->get('user/profile''User::profile', ['filter' => 'role:Users']);  //Works Correctly
}) 



RE: Myth/Auth - Routing problem - BilltheCat - 04-13-2020

Ahha, thanks again! "The group name becomes a segment that appears prior to the routes defined inside of the group." That cleared it up for me. Smile


RE: Myth/Auth - Routing problem - Nome - 04-13-2020

My problem is of a similar nature, so I did not begin to create a new topic.
I still use modular code, it is very convenient. But now I am faced with an incomprehensible problem.

I have a user module, in its config there is a route that consists of a simple group:

PHP Code:
$routes->group('user', ['namespace' => 'Modules\User\Controllers'], function($routes)
{

    $routes->get('/',       'User::index');
    $routes->post('add',    'User::createUser');
}); 

After my form(method="POST") located at the address, site/user/, sends data to the address site/user/add, a message appears with the following text:

Code:
404 - File Not Found
Controller or its method is not found: User::add

It turns out that the get method works and the post does not ... 

Tell me what is my mistake? There is almost nothing in the APP\Co..\Routes.php except :
$routes->setTranslateURIDashes(false);
$routes->set404Override();
$routes->setAutoRoute(true);

Thanks!


RE: Myth/Auth - Routing problem - includebeer - 04-13-2020

I’m not sure if this is causing this, but you should set auto route to false if you are defining your own route.
It is looking for User:add instead of User::createUser, so I guess it’s the auto routing that gets in the way.

PHP Code:
$routes->setAutoRoute(false); 



RE: Myth/Auth - Routing problem - Nome - 04-13-2020

(04-13-2020, 06:39 AM)includebeer Wrote: I’m not sure if this is causing this, but you should set auto route to false if you are defining your own route.
It is looking for User:add instead of User::createUser, so I guess it’s the auto routing that gets in the way.

PHP Code:
$routes->setAutoRoute(false); 

Apparently not, because it changed to a value: 
Code:
Can't find a route for 'user/add'.

Environment: development says me:
Code:
DEFINED ROUTES
Method    Route            Handler
GET    user            \Modules\User\Controllers\User::index
POST    user/add    \Modules\User\Controllers\User::createUser
POST    user/load    \Modules\User\Controllers\User::load

In my : modules\User\Controllers\User.php

PHP Code:
    public function index() {
        return view('Modules\User\Views\Index');
    }

    public function createUser() {
        $model = new UserModel();
        $model->createUser($data = [
            'mail'        => $this->request->getPost('mail'),
            ...
            'timezone'    => $this->request->getPost('timezone')
        ]);
        return redirect()->back();
    



RE: Myth/Auth - Routing problem - includebeer - 04-13-2020

DId you add your module’s namespace in the Autoload file?