Phalcon router doesn't react to subfolders and namespace declaration

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 00:25:15

问题


So I've been reading a ton of stackoverflow and phalcon forum threads.. (I'm starting to hate this framework), but nothing seem to work and it doesn't explain why like Laravel does, for example.

I'm just trying to be able to operate with this application structure:

As you can see, all I want is to use namespaced controllers in subfolders to make more order for my code.

According to all explanations, here's my loader.php:

<?php

$loader = new \Phalcon\Loader();

/**
 * We're a registering a set of directories taken from the configuration file
 */
$loader->registerDirs(
    array(
        $config->application->controllersDir,
        $config->application->modelsDir,
        $config->application->pluginsDir
    )
)->register();

AFAIK, Phalcon should traverse all subfolders for not found classes when used via registerDirs.

Then I define my routes to specific controller after the main route to index controllers in base directory:

<?php

$router = new Phalcon\Mvc\Router(false);

$router->add('/:controller/:action/:params', array(
    'namespace' => 'App\Controllers',
    'controller' => 1,
    'action' => 2,
    'params' => 3,
));

$router->add('/:controller', array(
    'namespace' => 'App\Controllers',
    'controller' => 1
));

$router->add('/soccer/soccer/:controller', array(
    'namespace' => 'App\Controllers\Soccer',
    'controller' => 1
));

$router->add('/soccer/:controller/:action/:params', array(
    'namespace' => 'App\Controllers\Soccer',
    'controller' => 1,
    'action' => 2,
    'params' => 3
));

return $router;

And one of my controllers look like:

<?php  namespace App\Controllers\Soccer;

use App\Controllers\ControllerBase as ControllerBase;

class IndexController extends ControllerBase
{

    public function indexAction()
    {

    }

}

What's wrong here? Default top namespace is not registered? Am I missing something?

This just doesn't work. When I try to open myserver.com/soccer which I expect to go to app/controllers/soccer/IndexController.php, but instead it tells me:

SoccerController handler class cannot be loaded

Which basically means it's looking for SoccerController.php in /controllers directory and totally ignores my subfolder definition and routes.

Phalcon 1.3.0

Stuck on this for a week. Any help - Much appreciated.


回答1:


I was having a problem with loading the ControllerBase and the rest of the controllers in the controllers folder using namespaces. I was having a hard time since other example projects worked fine and I realized that I was missing a small detail in the despatcher declaration where I was supposed to setDefaultNamespace

(ref: https://github.com/phalcon/vokuro/blob/master/app/config/services.php)

$di->set('dispatcher', function () {

    $dispatcher = new Dispatcher();

    $dispatcher->setDefaultNamespace('App\Controllers');

    return $dispatcher;
});

or you can specify it directly on the routing declaration file like this

$router->add("/some-controler", array(
    'namespace' => 'App\Controllers'
    'controller' => 'some',
    'action' => 'index'
));

that should work as well, it might be a bit confusing at first with namespaces but once you get a hang of it you will love this extremely fast framework




回答2:


It looks like your namespaces have capitals

App\Controllers\Soccer

and your folder structure does not

app\controllers\soccer

In my app I have controllers with a namespace and I have just tried changing the case of the folders so they don't match and I get the missing class error.

Of course it does raise a question, how many controllers are you planning on having that namespacing them is worthwhile? Are you grouping controllers and action by function or content? I used to have loads of controllers in Zend, but now I have grouped them by function I only have 16 and I am much happier. e.g. I suspect soccer, rugby, hockey etc will probably be able to share a sport controller, articles, league positions etc will have a lot of data in common.

ps Don't give up on Phalcon! In my experience it is sooo much faster than any other PHP framework I have used it is worth putting up with a lot :)




回答3:


Are you sure Phalcon traverses subfolders when registering directories to the autoloader? Have you tried adding a line to your autoloader which explicitly loads the controllers\soccer directory?

Alternatively, if your soccer controller is namespaced, you can also register the namespace: "App\Controllers\Soccer" => "controllers/soccer/" with the autoloader.



来源:https://stackoverflow.com/questions/22743731/phalcon-router-doesnt-react-to-subfolders-and-namespace-declaration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!