We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Routing uppercase & lowercase

Hei,

I am playing around with Vökuró and stumbled upon a funny thing. When I open a link to a controller which is not present, I get routed to the error-page. But when I open a link to a controller which exists, but use a uppercase letter, the page just loads the layout.volt without content.

Example:

  • www.example.com/mycontroller/ routes to the page 'mycontroller' as expected

  • www.example.com/maicooontroller/ routes to the error-page as expected

  • www.example.com/mycONTroller/ seems to route to the page 'mycontroller', but without processing the {{ content }}

How can I influence the router and/or dispatcher to "strtolower($str);" the passed values of the URL?

greetings



7.0k
Accepted
answer

Hi,

if i've well understood you're problem you should see this:

https://docs.phalcon.io/en/latest/reference/routing.html#using-convertions

It shows how to convert the different variables you're getting in you're routes

Thanks, that helped me. :)

So my routes.php looks like this:

<?php
/*
 * Define custom routes. File gets included in the router service definition.
 * /:controller     /([a-zA-Z0-9_-]+)   Matches a valid controller name with alpha-numeric characters only
 */
$router = new Phalcon\Mvc\Router\Annotations(false);

$router->removeExtraSlashes(true);
$router->setDefaultController('index');
$router->setDefaultAction('index');

/**
 * Standard MVC routes
 */
$router->add(
    '/([a-z0-9_-]+)/:action/:params',
    [
        'controller' => 1,
        'action'     => 2,
        'params'     => 3
    ]
);

return $router;

Now I get routed to /index/index when I open "www.example.com/mycONTroller/". I'll have to fix this, but that was the right direction. :) Thank you!

edited Nov '14

Well, I think routing is the wrong vector here. The controller is found either way. But it is the name of the corresponding view-folder that makes the difference.

So, www.example.com/mycONTroller/ won't display any content as the view-folder is named "mycontroller". As soon as I rename this folder "mycONTroller" the content is shown again. But then again the original link "mycontroller" can not be shown.

My guess is to change the logic that handles the view itself so it becomes case-insensitive. In my Windows-Testserver it is working as the filesystem is not case-sensitive. Different story on a *NIX system.

Any ideas on that?

//edit: I was testing a bit on the original Vokuro.

Vokuro does not check the foldernames of the views-folders. While https://vokuro.phalcon.io/users is not accessible without logging in, https://vokuro.phalcon.io/uSers (uppercase S) delivers some private content such as the menu for logged in users.

Hi again,

Did you try to disable the default behavior ? That's the first thing, i do when it start a new phalcon project, because i can handle things as i want, maybe you should try ;)



103

I`ve solved this way:

class ControllerBase extends Controller {
  public function beforeExecuteRoute(Dispatcher $dispatcher) {
    $dispatcher->setControllerName(strtolower($dispatcher->getControllerName()));
    $dispatcher->setActionName(strtolower($dispatcher->getActionName()));