Zend_Controller_Router_Route and creating new routes
recently on the Polish forum for Zend Framework I met with the problem of clean URLs. This library supports the standard addresses of the form:
http://nazwa_strony/nazwa_kontrolera/nazwa_akcji/parametr/wartość However
page was to contain a multi-lingual content. And although Zend supports the ability to read information from the browser developer is wanted to control the tongue by placing the code before the name of the controller, for example:
http://nazwa_strony/pl/nazwa_kontrolera/ ...
therefore decided to consult the documentation and write the necessary code. We'll start with the directory structure, which is taken from a tutorial:
application
controllers
IndexController.php
Tuesday, April 3, 2007
Does A Chest Infection Affect Fetus
views
library
Zend
Zend.php
-
-
RewriteEngine on RewriteRule .* index.php -
-
/ / Set debug - define ('_DEBUG', 1);
-
if (defined ('_DEBUG')) -
error_reporting (E_ALL -
try {/ / Load the necessary classes - Zend:: loadClass ('Zend_View');
public
- . htaccess index.php
Zend:: loadClass ('Zend_Controller_Front');
Zend:: loadClass ('Zend_Controller_Router_Rewrite');
/ / Set the base address
$ baseurl = substr ($ _SERVER ['PHP_SELF'], 0,
strpos ($ _SERVER ['PHP_SELF'], '/ index.php'));
/ / Configuration class
view $ view = new Zend_View ();
$ view-> baseurl = $ baseurl;
$ view-> setScriptPath ('. / Application / views');
Zend:: register ('view', $ view);
/ / Here we configure the new route
$ route = new Zend_Controller_Router_Route (
': lang /: controller /: action',
/ / Defines
language in front of the controller
array ('lang' => 'en', 'controller' => 'index',
'action' => 'index'), / / \u200b\u200bdefault values \u200b\u200b
array ('lang'=>'[ a-z_ ]+')); / / Condition for lang
/ / Create a router to route our
$ router = new Zend_Controller_Router_Rewrite ();
$ router-> removeDefaultRoutes ();
$ router-> addRoute ('user', $ route );
/ / Initialize the main controller
$ FrontController = Zend_Controller_Front:: getInstance ();
$ FrontController-> setRouter ($ router);
$ FrontController-> setBaseUrl ($ baseurl);
$
FrontController-> setControllerDirectory (
'. / Application / controllers');
$ FrontController-> throwExceptions (true);
$ FrontController-> returnResponse (true);
/ / Initialize the response object
$ response = $ FrontController-> dispatch ();
$ response-> renderExceptions (true);
$ response-> setHeader ('Pragma', 'no-cache');
$ response-> setHeader ('Cache-Control', 'no- cache ');
/ / Display the
echo $ response;} catch
(Exception $ exception) {
/ / Operation of exception
$ message = 'setup
Application exception:
';
$ message .= $ exception-> getMessage ();
if (defined ('_DEBUG') ) if (_DEBUG) {
$ message .= '
Trace:
'. $ exception-> getTraceAsString ();}
die ($ message);}
There remains therefore nothing but learn about the language. This is done in the file IndexController.php:
class IndexController extends Zend_Controller_Action
{
indexAction function () {
echo $ this-> _getParam ('lang', 'en');
echo "in IndexController: indexAction ();}
}
When copying files to the server will get information about the language and developed a method
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment