php - Codeigniter 4 doesn't locate default controller in subdirectory with underscore for hyphenated url
Get the solution ↓↓↓I have an earlier post on the Codeigniter forum 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:
Then I have to use a dash in the file system directory name. That url works if I put my controller in this location:
{-code-2}
Note that the properly working Home controller uses an underscore in its namespace:
{-code-3}
That same url {-code-5} not work if I put it in a directory with an underscore:
{-code-4}
Curiously, it the dash in the url I request{-code-5}
get properly routed to an underscore while CI4 is calculating the controller path. However, CI4 {-code-5}n't bother to check if the url is requesting the default Home controller in a subdirectory. The complaint it gives is:
Controller or its method is not found: \App\Controllers\Subdir\Nested_directory::index
So it almost works, is just {-code-5}n't bother checking for the default Home 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-6}
Connects correctly to the controller here:
{-code-4}
Previously, it was enough to simply make sure I name my subdirectories with dashes so they match the urls, however this {-code-5} not work when i must add a $routes->add directive mapping urls to a controller that happens to live in a subdirectory with a dash. This route directive works just fine in my Config/Routes file if Nested_directory contains an underscore:
{-code-8}
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
{-code-2}
then this will not work:
{-code-8}
This will also not work:
{-code-11}
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 where I must route all of my many controllers. The auto-routing of CI3 was working just fine. I think this problem could be remedied with a small code change.
Can anyone point me to a solution?
For reference, here is my Config/Routes.php file
// 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';
}
/**
*
Answer
Answer
Answer
Answer
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.