Php zend框架2+;路由数据库
您好,zend framework 2有一个问题,例如,我想要一个创建路由数据库Php zend框架2+;路由数据库,php,zend-framework2,Php,Zend Framework2,您好,zend framework 2有一个问题,例如,我想要一个创建路由数据库 /hello/index => is Application/Controllers/HomeController /CustomURL => is Application/Controllers/HomeController 我从数据库i中检索到的CustomUrl是我的配置文件 /// module.config.php 'router' => array( 'routes' =
/hello/index => is Application/Controllers/HomeController
/CustomURL => is Application/Controllers/HomeController
我从数据库i中检索到的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',
),
),
),
),
),....
这是我的路由器课程
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);
}
}
我非常noob和我需要一些帮助完成这部分谢谢
*更新*
我想要一些像邮政的
我的路由器看起来像这样:
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();
}
}
我这样称呼它:
'router' => array(
'routes' => array(
'content' => array(
'type' => 'Module\Router\Content',
'options' => array(
'defaults' => array(
'controller' => 'Module\Controller\Content',
'action' => 'view'
)
)
)
)
),
在控制器的viewAction()中,可以放置任何需要的内容。
我希望这有帮助
(我从某人那里借用了这个解决方案,但我再也记不起它的来源了。:()我的路由器看起来像这样:
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();
}
}
我这样称呼它:
'router' => array(
'routes' => array(
'content' => array(
'type' => 'Module\Router\Content',
'options' => array(
'defaults' => array(
'controller' => 'Module\Controller\Content',
'action' => 'view'
)
)
)
)
),
在控制器的viewAction()中,可以放置任何需要的内容。
我希望这有帮助
(我从某个人那里借用了这个解决方案,但我再也记不起它的来源了。:()我真的不清楚你问了什么,我假设你正在尝试创建路由,如果它不符合你的“/节点”路由它必须重定向到某个XYZ页面?是的,我想创建一个路由,但通过数据库,我真的不清楚您的问题,我假设您正在尝试创建路由,以便在它不符合您的“/节点”时路由它必须重定向到某个XYZ页面?是的,我想创建一个路由,但通过数据库我相信它来自这里:我相信它来自这里: