Welcome Guest, Not a member yet? Register   Sign In
Myth/Auth - Routing problem
#1

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
}); 
Reply
#2

(This post was last modified: 04-12-2020, 06:20 PM by includebeer.)

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
}); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

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

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

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
}) 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#6

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

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

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); 
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#9

(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();
    
Reply
#10

DId you add your module’s namespace in the Autoload file?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB