phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.97k forks source link

[BUG]: Full namespace class not found inside route annotations #16238

Open Jeckerson opened 1 year ago

Jeckerson commented 1 year ago

Describe the bug

Controller suffix is repeated when full classname is specified.

Fatal error: Uncaught ReflectionException: Class "App\Controllers\InvoicesControllerController" does not exist

To Reproduce

<?php

use App\Controllers\InvoicesController;
use Phalcon\Mvc\Router\Annotations;

$container['router'] = function () {
    $router = new Annotations(false);

    $router->addResource(Invoices::class, '/admin/invoices');

    return $router;
};

Expected behavior No error when specified full namespace class.

Details

SerginhoLD commented 1 year ago

Please remove all suffixes and namespaces in Router and Dispatcher. This is bad code.

https://github.com/phalcon/cphalcon/blob/a99d57ed4ba94b9d608a3d22d69ef871970086e7/phalcon/Dispatcher/AbstractDispatcher.zep#L945

Please change the code to full use of class and method names.

I don't want to have to specify a namespace all the time:

class Dispatcher extends \Phalcon\Mvc\Dispatcher
{
    protected $handlerSuffix = '';

    protected $actionSuffix = '';

    protected $defaultAction = '__invoke';

    /**
     * Fix Controller::__invoke()
     */
    public function getActiveMethod(): string
    {
        return $this->getActionName() . $this->getActionSuffix();
    }

    /**
     * Fix controller namespace
     */
    public function forward(array $forward): void
    {
        if (isset($forward['controller']) && !isset($forward['namespace'])) {
            $parts = explode('\\', $forward['controller']);

            if (count($parts) > 1) {
                $forward['controller'] = array_pop($parts);
                $forward['namespace'] = implode('\\', $parts);
            }
        }

        parent::forward($forward);
    }
}