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

Bug: Routes and Namespace #2423

Closed
yrneh-mi opened this issue Nov 21, 2019 · 0 comments · Fixed by #2425
Closed

Bug: Routes and Namespace #2423

yrneh-mi opened this issue Nov 21, 2019 · 0 comments · Fixed by #2425
Labels
bug Verified issues on the current code behavior or pull requests that will fix them

Comments

@yrneh-mi
Copy link

yrneh-mi commented Nov 21, 2019

Bug?
Namespace in routes. Value set in $routes->setDefaultNamespace('App\Controllers'); is not used when doing $routes->get('/', 'Core\Dashboard::index');.

CodeIgniter 4 version
CodeIgniter 4.0.0.0-rc3

Affected module(s)
Routing

Expected behavior, and steps to reproduce if appropriate
The controller app/Controller/Core/Dashboard.php:

<?php namespace App\Controllers\Core;

use App\Controllers\BaseController;

class Dashboard extends BaseController
{
    public function index()
    {
        return 'Hello';
    }
}

Using it like this in app/Config/Routes.php:

$routes->setDefaultNamespace('App\Controllers');
$routes->get('/', 'Core\Dashboard::index'); // Core is the namespace
$routes->get('/one', 'Core\ExampleOne::index'); // Core is the namespace
$routes->get('/two', 'Testing\ExampleTwo::index'); // Testing is the namespace

Expected output:

App\Controllers\Core\Dashboard::index
App\Controllers\Core\ExampleOne::index
App\Controllers\Testing\ExampleTwo::index

Actual output:

Core\Dashboard::index
Core\ExampleOne::index
Testing\ExampleTwo::index

This would be an error and would be a 404 page not found. The value set in $routes->setDefaultNamespace('App\Controllers'); is not used.

This works though (but....):

$routes->setDefaultNamespace('App\Controllers'); // Not used at all

$routes->get('/', 'App\Controller\Core\Dashboard::index');
$routes->get('/one', 'App\Controller\Core\ExampleOne::index');
$routes->get('/two', 'App\Controller\Testing\ExampleTwo::index');

or

$routes->setDefaultNamespace('App\Controllers'); // Not used at all unless below $baseNamespace is used

$baseNamespace = $routes->getDefaultNamespace();

$routes->get('/', $baseNamespace . '\Core\Dashboard::index');
$routes->get('/one', $baseNamespace . '\Core\ExampleOne::index');
$routes->get('/two', $baseNamespace . '\Testing\ExampleTwo::index');

or

$routes->setDefaultNamespace('App\Controllers'); // Not used at all

$routes->get('/', 'Dashboard::index', ['namespace' => 'App\Controllers\Core']);
$routes->get('/one', 'ExampleOne::index', ['namespace' => 'App\Controllers\Core']);
$routes->get('/two', 'ExampleTwo::index', ['namespace' => 'App\Controllers\Testing']);

// $routes->group() also works
// $routes->group('', ['namespace' => '', function ($routes) {}]);

Context

  • OS: Windows 10 Pro 64bit
  • Web server: N/A
  • PHP version: 7.3.10
@yrneh-mi yrneh-mi added the bug Verified issues on the current code behavior or pull requests that will fix them label Nov 21, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant