John Davidson

php - Codeigniter 4 doesn't locate default controller in subdirectory with underscore for hyphenated url

0 comments
Message:


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:


https://www.example.com/subdir/nested-directory


Then I have to use a dash 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 the properly working Home controller uses an underscore in its namespace:


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 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:



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 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:


https://www.example.com/subdir/nested_directory


Connects correctly to the controller here:


app/Controllers/Subdir/Nested_directory/Home.php


Previously, it was enough to simply make sure I name my subdirectories with dashes so they match the urls, however this does 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:


$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


then this will not work:


$routes->add("foobar/([0-9]+)", "Subdir\Nested_directory\Home::index/$1");

This will also not work:


$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 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';
}

/**
* --------------------------------------------------------------------
* 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");


My Answer:
CodeIgniter 4 is the latest version of the popular PHP framework that follows the Model-View-Controller (MVC) architectural pattern. It provides a lightweight and efficient way to build web applications.

To get started with CodeIgniter 4, you first need to install it using Composer. You can create a new project by running the following command in your terminal:


composer create-project codeigniter4/appstarter myproject


This will create a new CodeIgniter 4 project in a directory named `myproject`. You can then navigate to this directory and start developing your application.

CodeIgniter 4 comes with a powerful command-line interface (CLI) tool called `spark` that you can use to generate controllers, models, views, and other components. For example, to create a new controller, you can run the following command:


php spark make:controller MyController


This will create a new controller file named `MyController.php` in the `app/Controllers` directory.

You can define routes in the `app/Config/Routes.php` file to map URLs to controller methods. For example, to route the URL `/hello` to the `index` method of the `MyController` controller, you can add the following line to the routes file:


$routes->get('hello', 'MyController::index');


CodeIgniter 4 also comes with a powerful database library that you can use to interact with databases. You can configure your database connection in the `app/Config/Database.php` file and then use the Query Builder class to perform database operations.

Overall, CodeIgniter 4 provides a modern and efficient way to build web applications in PHP. It offers a clean and organized structure, powerful features, and excellent documentation to help you get started quickly.

Rate this post

3 of 5 based on 4433 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us