Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router Regex not working with controllers #2330

Closed
jbotka opened this issue Oct 14, 2019 · 5 comments
Closed

Router Regex not working with controllers #2330

jbotka opened this issue Oct 14, 2019 · 5 comments

Comments

@jbotka
Copy link
Contributor

jbotka commented Oct 14, 2019

Describe the bug
The problem seems to be in the router. My code is like this doesn't work. Error 404.

$routes->group('admin', ['namespace' => '\Admin\Controllers\Auth'], function ($routes){
    $routes->add('(.*)/(.*)', '$1/$2');
});

According to the guide, it should be possible to pass the controller and method name from URL during this. I tried and it works to pass it as method argument and display it in the controller.

It works like this to redirect everything from /admin/* to some defined controller in another namesapce defined like below.

$routes->group('admin', ['namespace' => '\Admin\Controllers\Auth'], function ($routes){
    $routes->add('(.*)', 'myController');
});

This doesn't worka as well. Error 404

$routes->group('admin', ['namespace' => '\Admin\Controllers\Auth'], function ($routes){
    $routes->add('(.*)/(.*)', '$1::$2');
});

Since it doesn't make log when accessing not existing controller. I am assuming it tries to pass segments as arguments to not defined controller all the time and can not pass it as controller name.

CodeIgniter 4 version
4.0.0 rc-2 - develop branch

Affected module(s)

  • Router

Expected behaviour, and steps to reproduce if appropriate
With this code, I expect to pass the first segment after "admin" as controller name and second segment as the method with the defined namespace. This should work according to the user guide.

$routes->group('admin', ['namespace' => '\Admin\Controllers\Auth'], function ($routes){
    $routes->add('(.*)/(.*)', '$1/$2');
});

Context

  • OS: Windows 8.1 x64
  • Apache 2.4.41
  • PHP 7.3.9
@lonnieezell
Copy link
Member

First off - thanks for filling out such a thorough bug report. That is much appreciated!

However, we recently revised the user guide to not show that anymore. Not only does it not work, it's not a good idea. See this issue for more details on why.

It sounds like you're wanting the CI3 style of auto-routing based on controller/method. Good news is - that still works, you just have to make sure your namespacing is correct for the controllers in your app/Controllers directory.

@jbotka
Copy link
Contributor Author

jbotka commented Oct 14, 2019

First off - thanks for filling out such a thorough bug report. That is much appreciated!

However, we recently revised the user guide to not show that anymore. Not only does it not work, it's not a good idea. See this issue for more details on why.

It sounds like you're wanting the CI3 style of auto-routing based on controller/method. Good news is - that still works, you just have to make sure your namespacing is correct for the controllers in your app/Controllers directory.

Thank You very much for the explanation but I cannot make it work anyway.
In my setup I have two namespaces.

  1. Admin - my shared 'app' that should have access controllers from any app
  2. App - my primary namespace accessing from index.php

if I want to make my app access controllers for Admin namespace it simply doesn't work. If I set properly controller in directory admin/Controllers it will throw 404 error and I have it in config autoload to include Admin namespace as well so I tried to do it this way.

In this case, it is more like module I do not understand why the user shouldn't be able to access any controller in a given namespace if I want to have it like that. If it would be my primary namespace it would do it anyway like this. All the time user can access any controller and method if I am not mistaken.

@fefo-p
Copy link

fefo-p commented Oct 15, 2019

Just a thought,
maybe you missed setting \Admin\Controllers\Auth path in \App\Config\Autoload.php, that is provided Admin is under App.
Like so:

$psr4 = [
	'Config'         => APPPATH . 'Config',
	APP_NAMESPACE    => APPPATH,  // For custom namespace
	'App'            => APPPATH,  // To ensure filters, etc still found,
	'Myth\Auth'      => APPPATH . 'ThirdParty/myth/auth/src',
        'Admin\Controllers\Auth' => APPPATH . 'directory_where_the_namespace_is',
		];

@jbotka
Copy link
Contributor Author

jbotka commented Oct 15, 2019

Just a thought,
maybe you missed setting \Admin\Controllers\Auth path in \App\Config\Autoload.php, that is provided Admin is under App.
Like so:

$psr4 = [
	'Config'         => APPPATH . 'Config',
	APP_NAMESPACE    => APPPATH,  // For custom namespace
	'App'            => APPPATH,  // To ensure filters, etc still found,
	'Myth\Auth'      => APPPATH . 'ThirdParty/myth/auth/src',
        'Admin\Controllers\Auth' => APPPATH . 'directory_where_the_namespace_is',
		];
        $psr4 = [
            'Config'            => APPPATH . 'Config',
            'Admin'             => ADMINPATH,
            'Admin\Auth'        => ADMINPATH. 'Auth',
            APP_NAMESPACE       => APPPATH,                // For custom namespace
            'App'               => APPPATH,                // To ensure filters, etc still found,
        ];

nope, this is set properly :) According to user guide this is not possible now. Auto-discovery is only for Events, Registrars, Route files, Services and controllers ave to be specified all the time that doesn't make sense. This restriction I do not understand why is here.

@fefo-p
Copy link

fefo-p commented Oct 15, 2019

I understand you set a namespace of Admin\Auth in autoload and in the Controller you're using Admin\Controllers\Auth (which I guess will not be found).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants