Phalcon多模块路由 [英] Phalcon Multi-Module Routing

查看:297
本文介绍了Phalcon多模块路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为Phalcon PHP框架使用多模块配置.

我有一个与路由相关的问题,因为我的模块之一无法通过url访问.似乎每次我尝试访问定义的模块uri时,它都会尝试调用默认的模块名称空间. Project\Module1\Controllers\Module2Controller handler class cannot be loaded

想法解决方案

url.com/module1  => module1
url.com/moudle2  => module2
url.com          => module1

在解决此问题时,我已验证所有路径和名称空间均正确.我用xdebug梳理了它.

我已经使用Phalcon Devtools创建了我的项目,该项目定义了多模块类型,因为它为我提供了要遵循的框架.当我使用此方法时,它提供了默认的路由方法,该方法解释每个注册的模块并将其绑定到url.这似乎不适用于我.这是解释之前的Phalcon项目. https://github.com/phalcon/phalcon-devtools/tree /master/templates/project/modules

路由

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/routes.php

$router = $di->get("router");

foreach ($application->getModules() as $key => $module) {
    $namespace = str_replace('Module','Controllers', $module["className"]);
    $router->add('/'.$key.'/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ))->setName($key);
    $router->add('/'.$key.'/:controller/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ));
    $router->add('/'.$key.'/:controller/:action/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ));
}

模块注册

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/modules.php

$application->registerModules(array(
    'module1' => array(
        'className' => 'Project\Module1\Module',
        'path' => __DIR__ . '/../apps/module1/Module.php'
    ),
    'module2' => array(
        'className' => 'Project\Module2\Module',
        'path' => __DIR__ . '/../apps/module2/Module.php'
    )
));

服务

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/services.php

$di->set('router', function () {
$router = new Router();

$router->setDefaultModule('module1');
$router->setDefaultNamespace('Project\Module1\Controllers');

 return $router;
});

Phalcon提供的示例.这与Phalcon工具实施不完全相同.

https://github.com/phalcon/mvc/tree/master/multiple-factory-default

非常感谢您的帮助.

解决方案

我在app\config\routes.php中定义了我的一般路由,然后在app\config\routes中,每个组(在本例中为模块)都有一个文件.

app \ config \ routes.php

<?php
$router = new \Phalcon\Mvc\Router();

$router->setDefaultModule("frontend");

$router->add("/:module/:controller/:action/:params", array(
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'params' => 4
));

foreach (glob(__DIR__ . "/routes/*.php") as $filename) {
    include $filename;
}

$router->removeExtraSlashes(true);

return $router;

admin \ config \ routes \ admin.php

<?php

use Phalcon\Mvc\Router\Group;

//Create a group with a common module and controller
$admin = new Group(array('module' => 'admin'));

$admin->setPrefix('/admin');

$admin->add("", array(
    'controller' => 'index',
    'action' => 'index'
));

$admin->add("/foo", array(
    'controller' => 'foo',
    'action' => 'index'
));

$admin->add("/bar", array(
    'controller' => 'bar',
    'action' => 'index'
));

$router->mount($admin);

我只需要定义控制器和索引,因为其他任何操作都将是url的一部分,并且应自行路由.

编辑

index.php

/*
 * ... includes
 */

$application = new \Phalcon\Mvc\Application($di);

$application->registerModules(array(
    'admin' => array(
        'className' => 'Calmsplash\App\Admin',
        'path' => '../app/admin/Admin.php',
    ),
    'frontend' => array(
        'className' => 'Calmsplash\App\Frontend',
        'path' => '../app/frontend/Frontend.php',
    ),
    /// ... more modules
));

我已经在模块名称后调用了我的Module文件,只是为了使其适合命名空间.

Im attempting to use the multi module configuration for the Phalcon PHP Framework.

Im having an issue relating to the routing as one of my modules is inaccessible via url. It seems every time I try to access the defined modules uri, it tries to invoke the default modules namespace. Project\Module1\Controllers\Module2Controller handler class cannot be loaded

The idea solution

url.com/module1  => module1
url.com/moudle2  => module2
url.com          => module1

In working to resolve this problem, I have verified that all paths and namespaces are correct. Ive combed through it with xdebug.

Ive created my project with Phalcon Devtools with the multi module type defined as it provided me with a skeleton to follow. As I am using this method, it provided a default routing method that interprets each registered module and binds it to a url. This does not seem to be working for me. This is the Phalcon project before interpretation. https://github.com/phalcon/phalcon-devtools/tree/master/templates/project/modules

Routing

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/routes.php

$router = $di->get("router");

foreach ($application->getModules() as $key => $module) {
    $namespace = str_replace('Module','Controllers', $module["className"]);
    $router->add('/'.$key.'/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 'index',
        'action' => 'index',
        'params' => 1
    ))->setName($key);
    $router->add('/'.$key.'/:controller/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 'index',
        'params' => 2
    ));
    $router->add('/'.$key.'/:controller/:action/:params', array(
        'namespace' => $namespace,
        'module' => $key,
        'controller' => 1,
        'action' => 2,
        'params' => 3
    ));
}

Module Registration

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/modules.php

$application->registerModules(array(
    'module1' => array(
        'className' => 'Project\Module1\Module',
        'path' => __DIR__ . '/../apps/module1/Module.php'
    ),
    'module2' => array(
        'className' => 'Project\Module2\Module',
        'path' => __DIR__ . '/../apps/module2/Module.php'
    )
));

Service

https://github.com/phalcon/phalcon-devtools/blob/master/templates/project/modules/services.php

$di->set('router', function () {
$router = new Router();

$router->setDefaultModule('module1');
$router->setDefaultNamespace('Project\Module1\Controllers');

 return $router;
});

An example provided by Phalcon. This not exact to the Phalcon Tools implementation.

https://github.com/phalcon/mvc/tree/master/multiple-factory-default

Any help is much appreciated.

解决方案

I have my general routes defined in app\config\routes.php then in app\config\routes I have one file for each of the groups (in this case modules).

app\config\routes.php

<?php
$router = new \Phalcon\Mvc\Router();

$router->setDefaultModule("frontend");

$router->add("/:module/:controller/:action/:params", array(
    'module' => 1,
    'controller' => 2,
    'action' => 3,
    'params' => 4
));

foreach (glob(__DIR__ . "/routes/*.php") as $filename) {
    include $filename;
}

$router->removeExtraSlashes(true);

return $router;

admin\config\routes\admin.php

<?php

use Phalcon\Mvc\Router\Group;

//Create a group with a common module and controller
$admin = new Group(array('module' => 'admin'));

$admin->setPrefix('/admin');

$admin->add("", array(
    'controller' => 'index',
    'action' => 'index'
));

$admin->add("/foo", array(
    'controller' => 'foo',
    'action' => 'index'
));

$admin->add("/bar", array(
    'controller' => 'bar',
    'action' => 'index'
));

$router->mount($admin);

I have only had to define the controller and index because any other actions will be part of the url and should route by themselves.

EDIT

index.php

/*
 * ... includes
 */

$application = new \Phalcon\Mvc\Application($di);

$application->registerModules(array(
    'admin' => array(
        'className' => 'Calmsplash\App\Admin',
        'path' => '../app/admin/Admin.php',
    ),
    'frontend' => array(
        'className' => 'Calmsplash\App\Frontend',
        'path' => '../app/frontend/Frontend.php',
    ),
    /// ... more modules
));

I've called my Module files after the module name just to fit them into the namespace.

这篇关于Phalcon多模块路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆