Zend Hostname Route dispatching defined controller / action - zend-framework

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');

Related

Get nested ressources when using cakePhp restfull

I'm using cakePhp to create a Rest api (see http://book.cakephp.org/2.0/fr/development/rest.html) and I need to get nested resources. The documentation tells how to get let's say books implementing a URI /books.json. But does not tell how to get for example reviews for a given book. What I'm trying to make is somthing like this: /books/14/reviews.json that returns Review resources.
Can any one tell me hwo to make this?
See the Custom REST Routing section of the docs you've linked. In case the default routing doesn't work for you, you'll have to create your own custom routes that either replace or extend the default ones.
Your /books/14/reviews.json URL could for example be mapped to BooksController::reviews() likes this:
Router::connect(
'/books/:id/reviews',
array(
'[method]' => 'GET',
'controller' => 'books',
'action' => 'reviews'
),
array(
'id' => Router::ID . '|' . Router::UUID,
'pass' => array(
'id'
)
)
);
When placed before Router::mapResources() it should work fine together with the default routes.

How to assemble a URL back to the www route when inside a Zend_Controller_Router_Route_Hostname route?

I have spent hours trying to find a solution to this but doesn't appear to be much out there.
I currently have admin.domain.com point to the admin module which is easy to do like so:
$adminRoute = new Zend_Controller_Router_Route_Hostname(
'admin.domain.com', array(
'module' => 'admin'
)
);
$router
->addRoute('admin', $adminRoute->chain(
new Zend_Controller_Router_Route(
':controller/:action/*',
array(
'controller' => 'index',
'action' => 'index'
))
));
And all other routes will work as per the default routing so that www.domain.com/module/controller and www.domain.com/controller both work as expected.
However, if I am currently inside the admin module and want to assemble a URL which points back to the www.domain.com route, then how does one do this?
Unfortunately it's not a simple case of making the default route a Hostname route for www.domain.com because then you don't benefit from the 'optional' module param in the path. E.g.
$wwwRoute = new Zend_Controller_Router_Route_Hostname(
'www.domain.com', array(
'module' => 'default'
)
);
$router
->addRoute('default', $wwwRoute->chain(
new Zend_Controller_Router_Route(
':module/:controller/:action/*',
array(
'controller' => 'index',
'action' => 'index'
))
));
Will simply build all paths in the format www.domain.com/default/controller/action rather than www.domain.com/controller/action
The docs suggest that the default routing uses Zend_Controller_Router_Route_Module but i've tried that and it seems there's more to than just that.
Any ideas?
EDIT: I'm suspecting that you either need to do one way or the other i.e. have all modules on a subdomain and then route the requests strictly like so /controller/action/*, or, have modules as part of the route path. Otherwise, a module would be reachable via two URLs - module.domain.com and www.domain.com/module - obviously there are ways round this, and of course, you could just set up specific routes for each module thereby not relying on the 'optional' module feature of the default Zend routing.

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 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?

Zend Route with subdomain yahoo like

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.