Yet Another Framework Yaf_Router 클래스

(Yaf >=1.0.0)


소개

Yaf_Router는 표준 프레임워크 라우터입니다.

라우팅은 URI 엔드포인트(기본 URI 뒤에 오는 URI 부분: Yaf_Request_Abstract::setBaseUri() 참조)를 가져와 매개변수로 분해하여 해당 컨트롤러의 모듈, 컨트롤러 및 작업이 요청을 수신해야 하는지 결정하는 프로세스입니다.

모듈, 컨트롤러, 작업 및 기타 매개변수의 이 값은 Yaf_Dispatcher에 의해 처리되는 Yaf_Request_Abstract 개체로 패키징됩니다. 라우팅은 요청이 처음 수신되고 첫 번째 컨트롤러가 발송되기 전에 한 번만 발생합니다.

Yaf_Router는 순수 PHP 구조를 사용하여 mod_rewrite와 유사한 기능을 허용하도록 설계되었습니다. Ruby on Rails 라우팅에 매우 느슨하게 기반하며 웹 서버 URL 재작성에 대한 사전 지식이 필요하지 않습니다. 단일 Apache mod_rewrite 규칙(다음 중 하나)과 함께 작동하도록 설계되었습니다.

예제 #1 Apache에 대한 재작성 규칙

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
                

또는 (선호):

예제 #2 Apache에 대한 재작성 규칙

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
                

Lighttpd를 사용하는 경우 다음 재작성 규칙이 유효합니다.

예제 #3 Lighttpd에 대한 재작성 규칙

url.rewrite-once = (
  ".*\?(.*)$" => "/index.php?$1",
  ".*\.(js|ico|gif|jpg|png|css|html)$" => "$0",
  "" => "/index.php"
)
                

Nginx를 사용하는 경우 다음 재작성 규칙을 사용하십시오.

예제 #4 Nginx용 재작성 규칙

server {
  listen ****;
  server_name  yourdomain.com;
  root   document_root;
  index  index.php index.html;

  if (!-e $request_filename) {
    rewrite ^/(.*)  /index.php/$1 last;
  }
}
                

Default route

Yaf_Router는 기본 경로 Yaf_Route_Static으로 사전 구성되어 제공되며, 이는 controller/action 형태의 URI와 일치합니다. 또한 모듈 이름을 첫 번째 경로 요소로 지정하여 module/controller/action 형식의 URI를 허용할 수 있습니다. 마지막으로 기본적으로 URI에 추가되는 모든 추가 매개변수(controller/action/var1/value1/var2/value2)와도 일치합니다.

메모: 모듈 이름은 application.module="Index,Foo,Bar"를 고려하여 config에서 정의해야 하며, 이 경우 index, foo, bar만 모듈 이름으로 간주할 수 있습니다. 구성하지 않으면 "색인"이라는 모듈이 하나만 있습니다.

이러한 경로가 일치하는 방법의 몇 가지 예는 다음과 같습니다.

예제 #5 Yaf_Route_Static(기본 경로) 예제

// Assuming the following configure:
$conf = array(
   "application" => array(
      "modules" => "Index,Blog",
   ),
);

Controller only:
http://example/news
    controller == news
Action only(when defined yaf.action_prefer=1 in php.ini)
    action  == news

Invalid module maps to controller name:
http://example/foo
    controller == foo

Module + controller:
http://example/blog/archive
    module     == blog
    controller == archive

Module + controller + action:
http://example/blog/archive/list
    module     == blog
    controller == archive
    action     == list

Module + controller + action + params:
http://example/blog/archive/list/sort/alpha/date/desc
    module     == blog
    controller == archive
    action     == list
    sort       == alpha
    date       == desc
                

클래스 개요

                  
class Yaf_Router {

  /* Properties */
  protected $_routes;
  protected $_current;

  /* Methods */
  public __construct()
  public addConfig(Yaf_Config_Abstract $config): bool
  public addRoute(string $name, Yaf_Route_Abstract $route): bool
  public getCurrentRoute(): string
  public getRoute(string $name): Yaf_Route_Interface
  public getRoutes(): mixed
  public route(Yaf_Request_Abstract $request): bool
}
                  
                

Properties

_routes
registered routes stack
_current
라우팅 단계 후에 이것은 현재 요청을 라우팅하는 데 사용되는 경로의 이름을 나타냅니다. Yaf_Router::getCurrentRoute()로 이 이름을 얻을 수 있습니다.

목차