Zend Framework zapewnia kilka alternatyw dla domyślnych klas, włączając w to alternatywne obiekty żądania, routery oraz obiekty odpowiedzi.
Zend_Controller_Request_Http
zapewnia obiekt
żądania do użycia w środowisku HTTP. Klasa
Zend_Controller_Request_Http
jest domyślną klasą
żądania używaną przez Zend_Controller_Dispatcher
.
Zend_Controller_Request_Http
obudowuje dostęp
do odpowiednich wartości takich jak nazwa klucza i wartość
dla zmiennych kontrolera i akcji routera, oraz do dodatkowych
parametrów pobranych z adresu URI. Rozszerzając
Zend_Controller_Request_Http
dodatkowo pozwala na
uzyskanie dostępu do wartości zawartych w superglobalnych
tablicach jako do publicznych właściwości obiektu i zarządza
obecnym bazowym adresem URL oraz adresem URL żądania.
Superglobalne wartości nie mogą być ustawione w obiekcie
żądania, zamiast tego użyj metod setParam/getParam aby ustawić
lub odebrać parametry użytkownika.
![]() |
Dane superglobalne |
---|---|
Kiedy uzyskujemy dostęp do danych superglobalnych za pomocą
klasy |
Konkretne zmienne superglobalne mogą być alternatywnie dostępne
za pomocą publicznej metody. Na przykład, wartość
$_POST['user']
może być dostępna przez wywołanie
metody getPost('user')
na obiekcie żądania.
Klasa Zend_Controller_Request_Http
pozwala na użycie
klasy Zend_Controller_Router_Rewrite w podkatalogach.
Zend_Controller_Request_Http spróbuje automatycznie wykryć twój
bazowy adres URL i ustawi go odpowiednio.
Na przykład jeśli twój plik index.php
jest w
podkatalogu nazwanym /projects/myapp/index.php
,
bazowy URL (bazowy adres przepisania) powinien być ustawiony na
/projects/myapp
. Ten łańcuch znaków zostanie
obcięty z początu ścieżki zanim będą dopasowane jakiekolwiek trasy.
To zwalnia z konieczności dołączania tego adresu do każdej z tras.
Trasa 'user/:username'
dopasuje adresy URI takie
jak http://localhost/projects/myapp/user/martel
oraz
http://example.com/user/martel
.
![]() |
Detekcja URL jest wrażliwa na małe i duże litery |
---|---|
Automatyczna detekcja adresów URL jest wrażliwa na małe i duże litery, więc upewnij się, że adres URL zostanie dobrze dopasowany do nazwy podkatalogu w systemie plików (nawet w systemie Windows). Jeśli nie zostanie, zostanie wywołana akcja noRoute. |
Jeśli bazowy adres URL jest wykrywany nieprawidłowo, możesz
go nadpisać w obiekcie Zend_Http_Request wywołując metodę
setBaseUrl()
lub tą samą metodę klasy
Zend_Controller_Request_Http
lub klasy
Zend_Controller_Front
. Najłatwiejszy sposób
to ustawienie tego w klasie Zend_Controller_Front
,
która przekaże to do obiektu żądania. Przykładowe użycie
ustawiania własnego bazowego adresu URL:
/** * Uruchom żądanie z własnym bazowym URL za pomocą Zend_Controller_Front. */ $router = new Zend_Controller_Router_Rewrite(); $controller = Zend_Controller_Front::getInstance(); $controller->setControllerDirectory('./application/controllers') ->setRouter($router) ->setBaseUrl('/projects/myapp'); // ustaw bazowy URL! $response = $controller->dispatch();
Zend_Controller_Router_Rewrite
jest standardowym
routerem we frameworku. Routing jest procesem pobrania adresu
URI i rozłożenia go w celu ustalenia jaki kontroler i jaka akcja
powinny otrzymać żądanie. Ta informacja o kontrolerze, akcji i
opcjonalnych parametrach jest pakowana do obiektu
Zend_Controller_Request_Http
, który jest potem
przetwarzany przez Zend_Controller_Dispatcher_Standard
.
Routing ma miejsce tylko raz: wtedy gdy żądanie jest po raz
pierwszy otrzymane, przed wywołaniem pierwszego kontrolera.
Zend_Controller_Router_Rewrite
jest zaprojektowany w
celu uzyskania w czystym PHP takiej funkcjonalności jak w
mod_rewrite. Jest to luźno wzorowane na routingu Ruby on Rails i
nie wymaga żadnej wiedzy o przepisywaniu adresów przez serwer
www. Jest to zaprojektowane w taki sposób, aby działało po
dodaniu tylko jednej reguły mod_rewrite (jednej z poniższych):
RewriteEngine on RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php
lub:
RewriteEngine on RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1
Rewrite router może być także użyty z serwerem IIS, jeśli moduł Isapi_Rewrite jest zainstalowany jako rozszerzenie Isapi, z taką regułą przepisywania:
RewriteRule ^[\w/\%]*(?:\.(?!(?:js|ico|gif|jpg|png|css)$)[\w\%]*$)? /index.php [I]
![]() |
IIS Isapi_Rewrite |
---|---|
Jeśli używasz serwera IIS, to wartość
|
Jeśli używasz serwera Lighttpd, poniższa reguła jest prawidłowa:
url.rewrite-once = ( ".*\.(js|ico|gif|jpg|png|css)$" => "$0", "" => "/index.php")
Aby prawidłowo użyć rewrite routera musisz utworzyć jego instancję, zdefiniować jakieś trasy i przekazać ten obiekt do kontrolera. Poniższy kod pokazuje tą procedurę:
/* Utwórz router */ $router = $ctrl->getRouter(); // domyślnie zwraca rewrite router $router->addRoute( 'user', new Zend_Controller_Router_Route('user/:username', array('controller' => 'user', 'action' => 'info')) );
Sercem RewriteRoutera jest definicja tras zdefiniowanych przez
użytkownika. Trasy są dodawane przez wywołanie metody addRoute
RewriteRoutera i przekazanie do niej nowej instancji
klasy implementującej interfejs
Zend_Controller_Router_Route_Interface
. Np.:
$router->addRoute('user', new Zend_Controller_Router_Route('user/:username'));
RewriteRouter posiada cztery podstawowe typy tras (z których jedna jeest specjalna):
Routes may be used numerous times to create a chain or user defined application routing schema. You may use any number of routes in any configuration, with the exception of Module route which should rather be used once and probably as the most generic route (ie. as a default). Każdy typ tras zostanie szerzej opisany w dalszej części rozdziału.
Pierwszy parametr do metody addRoute jest nazwą trasy. Jest on używany do obsługi pobierania tras z routera np. w celu wygenerowania URL. Drugi parametr jest samą trasą.
![]() |
Notatka |
---|---|
The most common use of the route name is through the means of Zend_View url helper: <a href="<?= $this->url('user', array('username' => 'martel')) ?>">Martel</a>
Co spowoduje utworzenie łącza: |
Routing is a simple process of iterating through all provided routes and matching it's definitions to current request URI. When a positive match is found, variable values are returned from the Route instance and are injected into Zend_Controller_Request object for later use in dispatcher as well as in user created controllers. On a negative match next route in chain is checked.
![]() |
Odwrotne dopasowanie |
---|---|
Trasy są dopasowane w odwrotnej kolejności, więc upewnij się, że najbardziej podstawowe trasy są zdefiniowane na początku. |
![]() |
Zwracane wartości |
---|---|
Wartości zwracane przez proces routingu pochodzą z parametrów URL lub z domyślnych wartości zmiennych zdefiniowanych w trasach. Te zmiene są później dostępne za pomocą metod Zend_Controller_Request::getParam oraz Zend_Controller_Action::_getParam. |
Są trzy specjalne zmienne, ktorych możesz użyć w swoich trasach - 'module', 'controller' oraz 'action'. Te specjalne zmienne są używane przez obiekt Zend_Controller_Dispatcher w celu znalezienia kontrolera i akcji do uruchomienia.
![]() |
Specjalne zmienne |
---|---|
Nazwy tych specjalnych zmiennych moga być inne jeśli
zdecydujesz się zmienić domyślne wartości w obiekcie
|
Zend_Controller_Router_Rewrite comes preconfigured with a
default route, which will match URIs in the shape of
controller/action
.
Additionally, a module name may be specified as the first path
element, allowing URIs of the form module/controller/action
.
Finally, it will also match any additional parameters appended to the URI by
default - controller/action/var1/value1/var2/value2
.
Kilka przykładów dopasowania tras:
// Zakładamy poniższe: $ctrl->setControllerDirectory( array( 'default' => '/path/to/default/controllers', 'news' => '/path/to/blog/controllers', 'blog' => '/path/to/blog/controllers' ) ); Tylko moduł: http://example/news module == news Nieprawidłowy moduł mapuje do nazwy kontrolera: http://example/foo controller == foo Moduł oraz kontroler: http://example/blog/archive module == blog controller == archive Moduł, kontroler oraz akcja: http://example/blog/archive/list module == blog controller == archive action == list Moduł, kontroler, akcja oraz parametry: http://example/blog/archive/list/sort/alpha/date/desc module == blog controller == archive action == list sort == alpha date == desc
Domyślna trasa jest po prostu obiektem
Zend_Controller_Router_Route_Module
przechowywanym
pod nazwą 'default' w RewriteRouterze. Jest ona utworzona mniej
więcej w taki sposób:
$compat = new Zend_Controller_Router_Route_Module(array(), $dispatcher, $request); $this->addRoute('default', $compat);
Jeśli nie chcesz mieć takiej domyślnej trasy w swoim schemacie
routingu, możesz nadpisać ją tworząc własną domyślną trasę
(np. zapisując ją pod nazwą 'default') lub usuwając tę trasę
używając metody removeDefaultRoutes()
:
// Usuwamy domyślne trasy $router->removeDefaultRoutes();
The rewrite router can be used in subdirectories (eg.
http://domain.com/~user/application-root/
>) in
which case the base URL of the application
(/~user/application-root
) should be automatically
detected by Zend_Controller_Request_Http
and used
accordingly.
Should the base URL be detected incorrectly you can override it
with your own base path with the help of
Zend_Controller_Request_Http
by calling the
setBaseUrl()
method (see
Sekcja 7.4.2.3, „Bazowy Url oraz podkatalogi”):
$request->setBaseUrl(/~user/application-root/);
Zend_Controller_Router_Route
is a standard framework
route which combines ease of use with flexible route definition.
Each route consists primarily of URL mapping (of static and dynamic
parts (variables)) and may be initialized with defaults as well as
with variable requirements.
Let's imagine our fictional application will need some
informational page about the content authors. We want to be able
to point our web browsers to
http://domain.com/author/martel
to see the
information about this "martel" guy. And route for such
functionality could look like:
$route = new Zend_Controller_Router_Route( 'author/:username', array('controller' => 'profile', 'action' => 'userinfo') ); $router->addRoute('user', $route);
First parameter in the
Zend_Controller_Router_Route
constructor is a route definition that will be matched to a URL.
Route definition consists of static and dynamic parts separated
by the slash ('/') character. Dynamic parts, called variables,
are marked by the colon prepended to the variable name (eg.
:username
). Static parts are just a simple text
(eg. author
).
![]() |
Użycie znaków |
---|---|
Current implementation allows you to use any characters (except a slash) as a variable identifier but it is strongly recommended that one uses only php variable friendly characters. Implementation may be altered in the future which could result in hidden bugs in your code. |
This example route should be matched when you point your browser to
'http://domain.com/author/martel'
in which case all it's
variables will be injected to the Zend_Controller_Request
object
and will be accessible in your ProfileController. Variables returned by
this example may be represented as an array of following key
and value pairs:
$values = array( 'username' => 'martel', 'controller' => 'profile', 'action' => 'userinfo' );
Later on Zend_Controller_Dispatcher
should invoke
userinfoAction method of your ProfileController class (in
default module) based on these values. There you will be able to
access all variables by the means of
Zend_Controller_Action::_getParam or
Zend_Controller_Request::getParam methods:
public function userinfoAction() { $request = $this->getRequest(); $username = $request->getParam('username'); $username = $this->_getParam('username'); }
Route definition can contain one more special character - a wildcard - represented by '*' symbol. It is used to gather parameters similarly to Module route (var => value pairs defined in the URI). Following route mimicks Module route behavior:
$route = new Zend_Controller_Router_Route(':controller/:action/*'); $router->addRoute('default', $route);
Every variable in the route can have a default and this is what
the second parameter of the
Zend_Controller_Router_Route
constructor is used
for. This parameter is an array with keys representing variable
names and with values as desired defaults:
$route = new Zend_Controller_Router_Route( 'archive/:year', array( 'year' => 2006 ) ); $router->addRoute('archive', $route);
Above route will match URLs like 'http://domain.com/archive/2005'
and
'http://example.com/archive'
. In the latter case the variable
year will have an initial default value of 2006.
This example will result in injecting a year variable to the
request object. And since no routing information is present (no
controller and action parameters are defined) application will
be dispatched to default controller and action method (which are
both defined in Zend_Controller_Dispatcher_Abstract
).
To make it more usable you have to provide a valid controller and
a valid action as route's defaults:
$route = new Zend_Controller_Router_Route( 'archive/:year', array( 'year' => 2006, 'controller' => 'archive', 'action' => 'show' ) ); $router->addRoute('archive', $route);
Taka trasa spowoduje uruchomienie akcji showAction z kontrolera ArchiveController.
One can add a third parameter to the Zend_Controller_Router_Route
constructor where variable requirements may be set. These are defined
as parts of a regular expression:
$route = new Zend_Controller_Router_Route( 'archive/:year', array( 'year' => 2006, 'controller' => 'archive', 'action' => 'show' ), array('year' => '\d+') ); $router->addRoute('archive', $route);
With route defined like above, Rewrite Router will match it
only when the year variable will contain numeric data, eg.
http://domain.com/archive/2345
. URL of
http://example.com/archive/test
will not be matched
and control will be passed to the next route in chain instead.
The examples above all use dynamic routes -- routes that contain patterns to match against. Sometimes, however, a particular route is set in stone, and firing up the regular expression engine would be an overkill. The answer to this situation is to use static routes:
$route = new Zend_Controller_Router_Route_Static( 'login', array('controller' => 'auth', 'action' => 'login') ); $router->addRoute('login', $route);
Powyższa trasa dopasuje URL http://domain.com/login
In addition to the default and static route types, a Regular Expression route type is available. This route offers more power and flexibility over the others, but at a slight cost of complexity. And should be faster than a standard Route at the same time.
As well as the standard route, this route has to be initialized with route definition and some defaults. Let's create an archive route as an example, similar to the previoulsy defined one, only using Regex route this time:
$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( 'controller' => 'archive', 'action' => 'show' ) ); $router->addRoute('archive', $route);
Every defined regex subpattern will be injected
to the request object. With our above example, after successful matching
http://domain.com/archive/2006
, the resulting value array
may look like:
$values = array( 1 => '2006', 'controller' => 'archive', 'action' => 'show' );
![]() |
Notatka |
---|---|
Leading and trailing slashes are trimmed from the URL in the Router
prior to a match. As a result, matching the URL |
![]() |
Notatka |
---|---|
Line start and line end anchors ('^' and '$', respectively) are automatically pre- and appended to all expressions. Thus, you should not use these in your regular expressions. |
![]() |
Notatka |
---|---|
This route class uses the |
You can get the contents of the defined subpatterns the usual way:
public function showAction() { $request = $this->getRequest(); $year = $request->getParam(1); // $year = '2006'; }
![]() |
Notatka |
---|---|
Zauważ, że klucz jest liczbą rzeczywistą (1), a nie łańcuchem znaków ('1'). |
This route will not yet work exactly the same as it's standard route counterpart since the default for 'year' is not yet set. And what may not yet be evident we will have a problem with a trailing slash even if we declare default for the year and make the subpattern optional. The solution is to make the whole year part optional along with the slash but catch only the numeric part:
$route = new Zend_Controller_Router_Route_Regex( 'archive(?:/(\d+))?', array( 'year' => '2006', 'controller' => 'archive', 'action' => 'show' ) ); $router->addRoute('archive', $route);
Now let's get to the problem you have probably noticed on your own by now. Using integer based keys for parameters is not an easily manageable solution and may be potentially problematic in the long run. And that's where the third parameter comes in. One which represents a map of regex subpatterns to parameter named keys. Let's work on our easier example:
$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( 'controller' => 'archive', 'action' => 'show' ), array( 1 => 'year' ) ); $router->addRoute('archive', $route);
This will result in following values injected into Request:
$values = array( 'year' => '2006', 'controller' => 'archive', 'action' => 'show' );
The map may be defined in both ways to make it work in any environment (eg. Zend_Config). Keys may contain variable names or subpattern numbers.
![]() |
Notatka |
---|---|
Subpattern keys have to be represented by integers |
$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( ... ), array( 1 => 'year' ) ); // LUB $route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( ... ), array( 'year' => 1 ) );
Notice that the numeric index in Request values is now gone and a named variable is shown in it's place. Of course you can mix numeric and named variables if you wish:
$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)/page/(\d+)', array( ... ), array( 'year' => 1 ) );
Which will result in mixed values available in the Request.
Example URL http://domain.com/archive/2006/page/10
will result in following values:
$values = array( 'year' => '2006', 2 => 10, 'controller' => 'archive', 'action' => 'show' );
Since regex is not easily reversed, you will need to prepare reverse URL if you wish to use an url helper or even an assemble method of this class. This reversed path is represented by a string parsable by sprintf() and is defined as a fourth construct parameter:
$route = new Zend_Controller_Router_Route_Regex( 'archive/(\d+)', array( ... ), array( 'year' => 1 ), 'archive/%s' );
All of this is something which was already possible by the means of
a standard route object, so where's the benefit in using Regex Route, you ask?
You can describe any type of URL without any restrictions.
Imagine you have a blog and wish to create URLs like:
http://domain.com/blog/archive/01-Using_the_Regex_Router.html
.
Now that's something which was not possible before, is it? Here's the solution:
$route = new Zend_Controller_Router_Route_Regex( 'blog/archive/(\d+)-(.+)\.html', array('controller' => 'blog', 'action' => 'view'), array(1 => 'id', 2 => 'description'), 'blog/archive/%d-%s.html' ); $router->addRoute('blogArchive', $route);
Czasem wygodniej jest uaktualnić plik konfiguracyjny z nowymi
trasami niż zmieniać kod. Jest to możliwe za pomocą metody
addConfig()
. Zasadniczo tworzysz konfigurację
kompatybilną z Zend_Config, a w kodzie odczytujesz ją i
przekazujesz ją do RewriteRoutera.
Przeanalizuj poniższy przykładowy plik INI:
[production] routes.archive.route = "archive/:year/*" routes.archive.defaults.controller = archive routes.archive.defaults.action = show routes.archive.defaults.year = 2000 routes.archive.reqs.year = "\d+" routes.news.type = "Zend_Controller_Router_Route_Static" routes.news.route = "news" routes.news.defaults.controller = "news" routes.news.defaults.action = "list" routes.archive.type = "Zend_Controller_Router_Route_Regex" routes.archive.route = "archive/(\d+)" routes.archive.defaults.controller = "archive" routes.archive.defaults.action = "show" routes.archive.map.1 = "year" ; OR: routes.archive.map.year = 1
Powyższy plik INI może być odczytany przez obiekt
Zend_Config
w taki sposób:
$config = new Zend_Config_Ini('/path/to/config.ini', 'production'); $router = new Zend_Controller_Router_Rewrite(); $router->addConfig($config, 'routes');
W powyższym przykładzie, nakazujemy routerowi użyć sekcji 'routes'
pliku INI aby użyć tras zdefiniowanych w tym pliku. Każdy element
pierwszego poziomu w tej sekcji będzie użyty do zdefiiniowania
nazwy trasy; powyższy przekład definiuje trasy 'archive' oraz
'news'. Wymagane jest aby każda trasa miała określony przynajmniej
parametr 'route' i jeden lub więcej parametrów 'defaults';
opcjonalnie mogą być zdeiniowane parametry 'reqs' (skrót
'required'). Wszystkie te parametry odpowiadają trzem argumentom
przekazywanym do obiektu Zend_Controller_Router_Route_Interface
Klucz opcji 'type' może być użyty aby określić typ
klasy, która ma być użyta dla danej trasy; domyślnie używana jest
klasa Zend_Controller_Router_Route
. W powyższym
przykładzie, trasa 'news' jest zdefiniowana aby używała
Zend_Controller_Router_Route_Static
.
Zend_Controller_Response_Http
jest obiektem odpowiedzi
odpowiednim do użycia w środowisku HTTP. Zawiera metody
do ustawiania, odbierania i czyszczenia nagłówków, a metoda
__toString()
wysyła wszystkie nagłówki na raz przed
wysłaniem zawartości odpowiedzi.
Metoda setHeader()
przyjmuje dwa argumenty, typ nagłówka
oraz wartość nagłówka. Trzeci opcjonalny parametr, jeśli jest
przekazany i ma wartość true, spowoduje, że nowy nagłówek zastąpi
inne zarejestrowane nagłówki o tym typie.