Zend Route with subdomain yahoo like - zend-framework

I would like to use Zend Route to treat URI like, http:/US.video.yahoo.com and also http://video.yahoo.com with the same route. For the second proposition i would like that the system tell me that the country that was to "US" in the first one, is now at NULL.
But i don't arrive to have Zend Route Hostname doing regex like stuffs.
How could i do that ?

To achieve routing on the hostname you need Zend_Controller_Router_Route_Hostname
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':country.:controller.yahoo.com',
array( 'action' => 'index' )
);
To combine your hostname route to other routes you need Zend_Controller_Router_Route_Chain
$staticRoute = new Zend_Controller_Router_Route_Static('');
$router->addRoute('default', $hostnameRoute->chain($staticRoute));
Check out the Zend framework manual, there are some good examples for you to follow.

Related

Zend Routing / URL helper . Have a global parameter show up first in URL

Was curious if anyone knew the best way to implement the following: I have a parameter in my zend framework 1.12 app which effectively controls the 'scope' of things, and is a field in every table in my db to represent the scope of a row. It is a simple integer variable, and can be thought of as 'buildingID', so it controls which 'building' we are working with.
In a plugin, I have:
Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('building', DYNAMIC_INT);
which accomplishes what I need. When I build a URL with the URL view-helper I have my parameter, but it is always at the end of the parameter list. I know this is trivial from a programming perspective, but how would I achieve 'prepending' this global param to my url parameters?
site.com/admin/controller/action/param1/xyz/param2/xyz/building/2
to become
site.com/admin/controller/action/building/2/param1/xyz/param2/xyz ?
Open to any ideas. If you want me to overload the url view helper, can you provide some example code, because I had trouble setting up this class.
Thank you all!
You can use a custom route to accomplish this. Setup the route somewhere in your bootstrap file:
$route = new Zend_Controller_Router_Route(
':controller/:action/building/:building/*'
);
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addRoute('building', $route);
And then, assuming that the following has been called at some point prior to using the url view helper...
Zend_Controller_Front::getInstance()->getRouter()->setGlobalParam('building', DYNAMIC_INT);
...you can specify the route as the second argument of the helper:
echo $this->url(array(
'controller' => 'admin',
'action' => 'controller',
'param1' => 'xyz',
'param2' => 'xyz',
), 'building');
// /admin/controller/building/1/param1/xyz/param2/xyz

Zend Routes translate URL's

1) I have a controller "calendar" and have action "showDate" which gets date via url. So, url is something like "calendar/show-date/date/2012-07-22"
2) I have a link to display all entries, "calendar/"
So, I want to create routes so my links look like "kalendar/2012-07-22" and "kalendar/".
Can anyone help me?
According to this post:
http://www.z-f.fr/forum/viewtopic.php?id=5138
The solution is to add '#locale' => $lang to the params.
$this->url(array('lang'=>'it','#locale'=>'it'))
It works very well for me.
I've been looking into translating the URL with Zend_Translate and I came across this sites' plugin that attempts to auto-translate URL segments (module/controller/action).
http://blog.helmich.cz/305-howto-simple-multilingual-routes-in-zend-framework/
The nice thing is that it's a modified custom router class that can function similar to Zend_Router so it's relatively familiar off the bat.
$pages = new MyApp_Controller_Router_Route(
':locale/:#controller/:#action/*',
array(
'controller' =>; 'index',
'action' => 'index',
'locale' => 'cs'
)
);
$router->addRoute('pages',$pages);
The thing you'll need is to have a language ID in your URL (called :locale in the above example) so your Zend_Translate can set the proper language.
www.example.com/en/calendar/2012-06-22/
www.example.com/fr/calendrier/2012-06-22/
www.example.com/de/kalender/2012-06-22/
www.example.com/it/calendario/2012-06-22/
I've only slightly played around with this concept but I recall that it had promise. You'll have to get more familiar with Zend_Translate: http://framework.zend.com/manual/en/zend.translate.html
I hope that helps!
Cheers!
You could re-route all calls of calendar to kalendar. There are two possibilites, either you do it with Zend (preferable) or you change your webserver configuration to rewrite calls to calendar with a HTTP 302 (ugly).
You should however consult the official Zend Documentation, which is pretty good
You have to setup custom routes, this is my way:
in folder application/configs/ create file named "routes.ini"
Put in file your route:
;index-homepage, parameter date isn't required
;"index" is key of your route
routes.index.route = "kalendar/:date"
routes.index.defaults.controller = calendar
routes.index.defaults.action = show
routes.index.defaults.date =
So in your bootstrap.php define that config file:
protected function _initRoute() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$router->addDefaultRoutes();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
$router->addConfig($config, 'routes');
}
And that's it, you can call URL
www.website.com/kalendar
and
www.website.com/kalendar/2012-1-1
See answers in this question for details:
Simple rewrites in Zend Framework

Zend Hostname Route dispatching defined controller / action

When a certain domain is requested, I would like to display page genereted by defined controller / action. I tried using hostname route (in this case requesting www.some-page.de should dispatch transportAction in IndexController), like this:
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
'www.some-page.de',
array(
'controller' => 'index',
'action' => 'transport'
)
);
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
$router->addRoute('transport', $hostnameRoute->chain($plainPathRoute));
Apparently I am doing something wrong, because it isn't working (instead indexAction of IndexController is being dispatched). Any hints or ideas how can I achieve this?
I've got it - it's very simple and I hope it is also good practice:
In a plugin's routeShutdown hook I check the domain name (using $_SERVER['HTTP_HOST']) and if it is the desired domain, I set the action name using following code:
$request->setActionName('transport');

Zend-Route for ajax API

I'm trying to add a route to my application, so that I can use it with ajax calls.
Here is what I have in my application.ini
;Routes
resources.router.routes.products.route = "/backend/api/:command"
resources.router.routes.products.defaults.module = "backend"
resources.router.routes.products.defaults.controller = "api"
resources.router.routes.products.defaults.action = "index"
When a ajax call is made, to /backend/api/SomeCommand, the following error is produced:
Message: Invalid controller specified (backend)
array (
'controller' => 'backend',
'action' => 'maestro',
'module' => 'default',
)
as you can see module has been set to "default", instead of "backend", and controller is "backend" instead of "api", what could have caused this?
Looks like you've got another more generic route defined after this one that's matching the request.
You need to define your routes in order of least to most specific, specificity usually being improved by the presence of fixed terms like your backend/api prefix.
See Basic Rewrite Router Operation, in particular
Note: Reverse Matching
Routes are matched in reverse order so make sure your most generic routes are defined first.
FYI: You don't need to prefix your routes with a forward-slash

Zend Framework - routes - all requests to one controller except requests for existing controllers

How to create route that accept all requests for unexsting controllers, but leave requests for existing.
This code catch all routes
$route = new Zend_Controller_Router_Route_Regex('(\w+)', array('controller' => 'index', 'action' => 'index'));
$router->addRoute('index', $route);
how should I specify route requests like /admin/* or /feedback/* to existing adminController or feedbackController?
You should not create a route to handle that. The error controller will take care of all three kinds of the following errors:
Controller does not exist
Action does not exsist
No route matched
Take a look at the documentation on how to use it correctly:
http://framework.zend.com/manual/en/zend.controller.plugins.html#zend.controller.plugins.standard.errorhandler.fourohfour
I found only the way - not to add route in case current request is about admin area
$request = $frontController->getRequest();
if (!preg_match('/knownController/', $request->getRequestUri())){
$router->addRoute('index', new Zend_Controller_Router_Route_Regex('(.*)', array('controller' => 'index', 'action' => 'index')));
}
You can also use the ErrorController to do a similar thing. Maybe if you dig into the way they implement the plugin it will help you build something that meets your needs closely?