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
#2

Why don't you just set the auto-route to false and define your own route?
PHP Code:
$routes->setAutoRoute(false); 


Then you can name your controllers and methods to whatever you want and it doesn't need to match what's in the URL.
There are still some rules to follow, like the first uppercase letter, but you have more flexibility this way.

http://codeigniter.com/user_guide/incomi...outes-only
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#3

(12-16-2020, 04:03 PM)includebeer Wrote: Why don't you just set the auto-route to false and define your own route?
Doesn't that necessitate the maintenance of a separate list of url mappings in the routes file? I.e., an extra step that, if done incorrectly, yields a broken url -- and also potentially a bottleneck if you have a bunch of developers adding controllers/methods that require such routing.

The solution I've mentioned is currently adequate. You just have to be careful to name the subdirectory in the Controllers folder using a hyphen and remember to use a underscore in the namespace declaration in your controllers.
Reply
#4

Yes that would necessitate to add all the routes in the config. With the routes config file you can also restrict routes by http method (GET, POST, etc) and add filters. There’s a lot more you can do. But if your solution works, then it’s all good!
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#5

Why app/Controllers/Sub-dir/Home.php ?
Should it be app/Controllers/Sub_dir/Home.php in PSR-4?
Reply
#6

(02-17-2021, 04:20 AM)kenjis Wrote: Why app/Controllers/Sub-dir/Home.php ?
Should it be app/Controllers/Sub_dir/Home.php in PSR-4?
Because, as I've described, I need *hyphens* in my url for SEO optimization. If you put the controller at app/Controllers/Sub_dir/Home.php, this url does not work:
Code:
https://www.example.com/sub-dir
uri dash translation doesn't work. You have to use an underscore in your url:
Code:
https://www.example.com/sub_dir
Reply
#7

Okay, I understand the current implementation does not accept `Sub_dir`.

I think `Sub_dir` and uri dash translation should accept `https://www.example.com/sub-dir`.
`app/Controllers/Sub-dir/Home.php` is not PSR-4 compliant.
Reply
#8

(02-17-2021, 04:43 PM)kenjis Wrote: Okay, I understand the current implementation does not accept `Sub_dir`.

I think `Sub_dir` and uri dash translation should accept `https://www.example.com/sub-dir`.
`app/Controllers/Sub-dir/Home.php` is not PSR-4 compliant.

Kenjis, I very much appreciate your participation in this discussion. It's helpful to know that `app/Controllers/Sub-dir/Home.php` is not PSR-4 compliant. I think I've located a bug in CI4 and I've reported it as Issue #4294. Skip over the main/first post to my second one that I posted a few minutes ago. I think this will explain what the bug is.
Reply
#9

I have submitted pull request #4307 to fix this issue.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB