Welcome Guest, Not a member yet? Register   Sign In
CI4 doesn't support hyphens/dashes in controller subdirectories
#1

(This post was last modified: 12-15-2020, 01:17 PM by sneakyimp.)

I'm migrating a CI3 site to CI4. This site has a bunch of subdirectories in the controllers folder that have hyphens in them (e.g., data-import, community-colleges, undergraduate-colleges, etc). Sadly, I can't figure out how to get CI4 to serve the controllers in these hyphenated subdirectories. I'd also point out that you apparently have to use a different namespace and appropriate use statements for any controller in a subdirectory but this is not mentioned at all in the documentation on controllers in subdirectories.

I started with just one subdirectory subdir. This controller inside app/Controllers/Subdir works:
PHP Code:
<?php
namespace App\Controllers\Subdir;

use \
App\Controllers\BaseController;

class 
Home extends BaseController
{
    public function 
index()
    {
        die(
"i am subdir");
    }


I tried changing the subdirectory app/Controllers/Subdir to app/Controllers/Sub_dir and used this code, but CI4 responds with a 404 when I try to access https://example.com/sub-dir/home/index:
PHP Code:
<?php
namespace App\Controllers\Sub_dir;

use \
App\Controllers\BaseController;

class 
Home extends BaseController
{
    public function 
index()
    {
        die(
"i am sub_dir");
    }


I *am* able to see the controller properly if I visit https://example.com/sub_dir/home/index (note the underscore) but I understand from Google's documentation that hyphens are better for SEO than underscores:
Quote:The URL http://www.example.com/green-dress.html is much more useful to us than http://www.example.com/greendress.html. We recommend that you use hyphens (-) instead of underscores (_) in your URLs.

It's my understanding that PHP namespaces and sub-namespaces must adhere to the conventions for any PHP label (starts with letter or underscore, may contain letters, numbers, and underscores) so a namespace therefore may not contain a hyphen. That being the case, and due to the PHP namespace constraints, we'd have to use underscores. Surely it would be possible to tweak the CI4 logic so that it can translate a hyphen in the url to an underscore in the controller's file path?

Is there some config setting I must change for CI4 to translate a hyphen in the url into a dash for the controller location?

I found a helpful-but-incomplete tip on Stack Exchange which has solved my problem. I apologize for not having done a more thorough search earlier. I hope this summary might help someone else.

To summarize:
* You should modify app/Config/Routes.php so this line has parameter of true:
PHP Code:
$routes->setTranslateURIDashes(true); 
* Your controller subdirectory should have a hyphen. My controller is located at app/Controllers/Sub-dir/Home.php
* Your controller's namespace should have an underscore and you should have a use statement for the BaseController. This is my example controller:
PHP Code:
<?php
namespace App\Controllers\Sub_dir;

use \
App\Controllers\BaseController;

class 
Home extends BaseController
{
 public function 
index()
 {
 die(
"i am subdir");
 }

* If your subdir in your file system has a hyphen, your url should have a hyphen. Access this controller at https://example.com/sub-dir/home/index

I hope someone else may find this useful.
Reply


Messages In This Thread
CI4 doesn't support hyphens/dashes in controller subdirectories - by sneakyimp - 12-15-2020, 01:01 PM



Theme © iAndrew 2016 - Forum software by © MyBB