Skip to content

Dispatcher tries to camel case twice #12829

Closed
@jesseforrest

Description

@jesseforrest

Expected and Actual Behavior

The following line in the Dispatcher can cause the controller name to be incorrectly camel cased two times, which can cause routes to not work:
https://github.com/phalcon/cphalcon/blob/master/phalcon/dispatcher.zep#L781

For single word controllers (e.g. User) this is not a problem, but for multiple word controllers (e.g. GiftCard) it is.

The problem arises when routes are defined in either of the following formats:

$router->add('/gift-card', ['controller' => 'GiftCard', 'action' => 'showGiftCard']);
$router->add('/gift-card', 'GiftCard::showGiftCard');

IMO, the real problem is that Phalcon Router class is trying to be too accommodating and allowing the controller to be passed in as GiftCard, giftCard or gift_card.

To solve the problem, without a change being made to Phalcon, I could have done 1 of 2 things.

The first thing was to change the controller names to be snake_case. Example:

$router->add('/gift-card', ['controller' => 'gift_card', 'action' => 'showGiftCard']);
$router->add('/gift-card', 'gift_card::showGiftCard');

I decided against this approach because engineers wouldn't be able to simply search across the code base for all uses of GiftCard anymore. I also didn't like this approach because it enforced that controllers were passed into the router in snake_case, but action names were camelCase.

The second option was to override the default Dispatcher behavior. This was the approach I took for now until this issue can be discussed and resolved:

<?php
use Phalcon\Mvc\Dispatcher;

class FixedDispatcher extends Dispatcher
{
    public function getHandlerClass(): string
    {
        parent::getHandlerClass();
        return $this->_handlerName . $this->_handlerSuffix;
    }
}

I will create another story for documentation too, since I feel like the following page should showcase more examples with multiple word controller/action names:
https://docs.phalconphp.com/en/3.0.2/reference/routing.html

Describe what you are trying to achieve and what goes wrong.

I think the documentation and code should be explicit and enforce a certain way. Ideally the way we pass in controller and action names would be similar.

Another side note, which is important for reproducing. This bug only appears if your source code is on a case-sensitive operating system (e.g. linux). It will not appear on mac, or if your code is mounted to a mac OS, because mac treats the following the same: GiftCardController.php, giftcardController.php, giFtCaRdCoNtroLLeR.php`, etc.

Details

  • Phalcon version: (php --ri phalcon)
Web framework delivered as a C-extension for PHP
phalcon => enabled
Author => Phalcon Team and contributors
Version => 3.0.3
Build Date => Dec 24 2016 19:16:23
Powered by Zephir => Version 0.9.5a-dev

Directive => Local Value => Master Value
phalcon.db.escape_identifiers => On => On
phalcon.db.force_casting => Off => Off
phalcon.orm.events => On => On
phalcon.orm.virtual_foreign_keys => On => On
phalcon.orm.column_renaming => On => On
phalcon.orm.not_null_validations => On => On
phalcon.orm.exception_on_failed_save => Off => Off
phalcon.orm.enable_literals => On => On
phalcon.orm.late_state_binding => Off => Off
phalcon.orm.enable_implicit_joins => On => On
phalcon.orm.cast_on_hydrate => Off => Off
phalcon.orm.ignore_unknown_columns => Off => Off
  • PHP Version: (php -v)
PHP 7.0.17-2+deb.sury.org~trusty+1 (cli) (built: Mar 15 2017 09:38:47) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.17-2+deb.sury.org~trusty+1, Copyright (c) 1999-2017, by Zend Technologies
  • Operating System:
Ubuntu
  • Installation type: Compiling from source || installing via package manager
  • Zephir version (if any):
  • Server: Apache
  • Other related info (Database, table schema):

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions