Welcome Guest, Not a member yet? Register   Sign In
Codeigniter 4 doesn't detect Home controller in subdirectory
#1

(This post was last modified: 02-16-2021, 04:58 PM by sneakyimp.)

I have an earlier post relating to this where I am trying to support URLs with dashes in them. Long story short, I must use dashes in my controller subdirectory names if I want to have dashes in my url. For whatever reason, the CI4 auto-routing is unable to recognize when a url with a dash in the path is referring to a controller subdirectory with an underscore.

E.g., if I want this url to connect to the default Home controller in my nested-directory subdirectory:
Code:
https://www.example.com/subdir/nested-directory
Then I have to use dashes in the file system directory name. That url works if I put my controller in this location:app/Controllers/Subdir/Nested-directory/Home.php. Note that Home controller uses an underscore in its namespace:
PHP Code:
namespace App\Controllers\Subdir\Nested_directory;

use 
App\Controllers\BaseController;

class 
Home extends BaseController {

    public function 
index()
    {
        die(
"this is " __METHOD__);
    }


That same url does not work if I put it in a directory with an underscore: app/Controllers/Subdir/Nested_directory/Home.php. Curiously, it looks like the dash in the url I request does get properly routed to an underscore while CI4 is calculating the controller path, however CI4 doesn't bother to check if the url is requesting the default Home controller in a subdirectory. The complaint it gives is:
Code:
Controller or its method is not found: \App\Controllers\Subdir\Nested_directory::index
.
So it almost works, is just doesn't bother checking for the default controller in a subdir. Additionally, if I change my url to have an underscore in it, then CI4 can locate the Home controller in the subdirectory named with an underscore. This url:
Code:
https://www.example.com/subdir/nested_directory
Connects correctly to the controller app/Controllers/Subdir/Nested_directory/Home.php.

In my prior post mentioned above, it was enough to just make sure I name my subdirectories with dashes so they match the urls, however this does not work when i try do a $routes->add to a controller that happens to live in a subdirectory with a dash. This works just fine in my Config/Routes file if [b]Nested_directory contains an underscore:
PHP Code:
$routes->add("foobar/([0-9]+)""Subdir\Nested_directory\Home::index/$1"); 
This serves a request for https://www.example.com/foobar/123 with the correct Home controller.

But if if the Nested-directory subdirectory containing my Home controller contains a dash, meaning my Home controller file is app/Controllers/Subdir/Nested-directory/Home.php, this will not work:
PHP Code:
$routes->add("foobar/([0-9]+)""Subdir\Nested_directory\Home::index/$1"); 
This will also not work:
PHP Code:
$routes->add("foobar/([0-9]+)""Subdir\Nested-directory\Home::index/$1"); 

Please note that I need to have these dashes instead of underscores because that is recommended by Google. I also very much do not want to create a big, complicated Routes file. The auto-routing of CI3 was working just fine. I think this problem could be remedied with a small code change.

For reference, here is my Config/Routes.php file
PHP Code:
<?php namespace Config;

// Create a new instance of our RouteCollection class.
$routes Services::routes();

// Load the system's routing file first, so that the app and ENVIRONMENT
// can override as needed.
if (file_exists(SYSTEMPATH 'Config/Routes.php'))
{
 require 
SYSTEMPATH 'Config/Routes.php';
}

/**
 * --------------------------------------------------------------------
 * Router Setup
 * --------------------------------------------------------------------
 */
$routes->setDefaultNamespace('App\Controllers');
$routes->setDefaultController('Home');
$routes->setDefaultMethod('index');
$routes->setTranslateURIDashes(true);
$routes->set404Override();
$routes->setAutoRoute(true);

/**
 * --------------------------------------------------------------------
 * Route Definitions
 * --------------------------------------------------------------------
 */

// We get a performance increase by specifying the default
// route since we don't have to scan directories.
$routes->get('/''Home::index');

// neither of these work if the controller is in Nested-directory
//$routes->add("foobar/([0-9]+)", "Subdir\Nested-directory\Home::index/$1");
$routes->add("foobar/([0-9]+)""Subdir\Nested_directory\Home::index/$1"); 
Reply




Theme © iAndrew 2016 - Forum software by © MyBB