2013-08-05 2 views
1

안녕하세요이 내가 예를젠드 프레임 워크 2 + 라우팅 데이터베이스

/hello/index => is Application/Controllers/HomeController 
/CustomURL => is Application/Controllers/HomeController 

내가 데이터베이스에서 검색하려면 CustomUrl에 대한 작성 라우팅 데이터베이스를 원하는 젠드 프레임 워크 2와 problemn 내가 여기에 내 구성 파일입니다

/// 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); 
    } 
} 

메신저 매우 멍청한 놈이고 내가 할이 부분에 대한 도움이 필요합니다 감사

* 업데이트는 좀 같이 게시 할 * 내 라우터는 다음과 같습니다 Tutorials For Database-Driven Routing in Zend Framework?

+0

, 내가 생각하는 것은 당신이 그렇게 라우팅을 만들려고한다는 것입니다 그것은 "/ 노드"경로를 충족시키지 않으면 일부 XYZ 페이지로 리디렉션해야합니까? –

+0

네 라우팅을 만들지 만 데이터베이스로 만듭니다 – user2646690

답변

1

:

'router' => array(
    'routes' => array(
     'content' => array(
      'type' => 'Module\Router\Content', 
      'options' => array(
       'defaults' => array(
        'controller' => 'Module\Controller\Content', 
        'action' => 'view' 
       ) 
      ) 
     ) 
    ) 
), 
:

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(); 
} 

}

내가 이런 식으로 호출

컨트롤러의 viewAction()에서 원하는 것을 넣을 수 있습니다. 도움이되기를 바랍니다.

(I이 솔루션 frome 크로스 누군가를 빌려,하지만 난 더 이상 소스를 불러올 수 없습니다. :() 정말 명확하게 질문을 이해하지 않았다

+2

나는이 사이트가 http://www.zendexperts.com/2012라고 믿습니다./12/09/custom-routing-in-zend-framework-2 / –