[英]zend framework 2 + routing database
hi have a problemn with zend framework 2 i want a create routing database for example 嗨,您对zend Framework 2有疑问吗?例如,我想创建一个路由数据库
/hello/index => is Application/Controllers/HomeController
/CustomURL => is Application/Controllers/HomeController
the CustomUrl i retrieve from database i here is my configuration file 我从数据库检索的CustomUrl,这是我的配置文件
/// module.config.php
'router' => array(
'routes' => array(
.....
'node' => array(
'type' => 'Application\Router\Page',//src/Application/Router/Page.php
'options' => array(
'route' => '/node',
'defaults' => array(
'controller' => 'Application\Controller\Index',
'action' => 'index',
),
),
),
),
),....
here is my Router class 这是我的路由器课
namespace Application\Router;
use Zend\Mvc\Router\Exception;
use Zend\Stdlib\ArrayUtils;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Mvc\Router\Http;
use Zend\Mvc\Router\Http\Literal;
class Page extends Literal
{
protected $routePluginManager = null;
protected $defaults = array();
public function match(Request $request, $pathOffset = null)
{
$uri = $request->getUri();
$path = $uri->getPath();
//sample logic here
//for /about/gallery uri set node id to 1
//todo: get action, controller and module from navigation
if($path == '/node'){
$uri->setPath('/node/1');
$request->setUri($uri);
}
return parent::match($request, $pathOffset);
}
protected function buildPath(array $parts, array $mergedParams, $isOptional, $hasChild)
{
if(isset($mergedParams['link']))
{
return $mergedParams['link'];
}
return parent::buildPath($parts, $mergedParams, $isOptional, $hasChild);
}
}
im very noob and i need some help for done this part thanks 即时消息非常菜鸟,我需要帮助完成此部分,谢谢
* update * i want some like post Tutorials For Database-Driven Routing in Zend Framework? *更新*我想要一些类似Zend Framework中的数据库驱动路由的教程?
My router looks like this: 我的路由器如下所示:
class Content implements RouteInterface,ServiceLocatorAwareInterface
{
protected $defaults = array();
protected $routerPluginManager = null;
public function __construct(array $defaults = array())
{
$this->defaults = $defaults;
}
public function setServiceLocator(ServiceLocatorInterface $routerPluginManager)
{
$this->routerPluginManager = $routerPluginManager;
}
public function getServiceLocator()
{
return $this->routerPluginManager;
}
public static function factory($options = array())
{
if ($options instanceof \Traversable) {
$options = ArrayUtils::iteratorToArray($options);
} elseif (! is_array($options)) {
throw new InvalidArgumentException(__METHOD__ . ' expects an array or Traversable set of options');
}
if (! isset($options['defaults'])) {
$options['defaults'] = array();
}
return new static($options['defaults']);
}
public function match(Request $request,$pathOffset = null)
{
if (! method_exists($request,'getUri')) {
return null;
}
$uri = $request->getUri();
$fullPath = $uri->getPath();
$path = substr($fullPath,$pathOffset);
$alias = trim($path,'/');
$options = $this->defaults;
$options = array_merge($options,array(
'path' => $alias
));
return new RouteMatch($options);
}
public function assemble(array $params = array(),array $options = array())
{
if (array_key_exists('path',$params)) {
return '/' . $params['path'];
}
return '/';
}
public function getAssembledParams()
{
return array();
}
} }
I call it this way: 我这样称呼它:
'router' => array(
'routes' => array(
'content' => array(
'type' => 'Module\Router\Content',
'options' => array(
'defaults' => array(
'controller' => 'Module\Controller\Content',
'action' => 'view'
)
)
)
)
),
In viewAction() in the controller, you can put whatever you want. 在控制器的viewAction()中,您可以放置任何内容。 I hope this helps. 我希望这有帮助。
(I have borrowed this solution frome somebody, but I cannot recall the source anymore. :( ) (我已经从某人那里借用了此解决方案,但是我再也无法回忆起来源了。:()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.