Zend View: Domain to Action - zend-framework

I am looking for a method in the bootstrap that looks at the URL then loads a specific module/controller/action.
However I don't want the user to see it.
Is this possible and easy?

I'm unsure wether you are looking for Hostname Routing, or just normal Routing, so I will answer both.
Zend_Controller_Router_Route
ie domain.com/users/layke
Using Zend_Controller_Route you can create a route will will grab "layke" and use that as a parameter. This would be a a standard Route.
$route = new Zend_Controller_Router_Route(
'users/:username',
array(
'controller' => 'profile',
'action' => 'users'
)
);
$router->addRoute('user', $route);
Then also there is hostname matching....
Zend_Controller_Router_Route_Hostname
For instance, (as the guides provide)...
You could use..
:username.domain.com to map to
/default/users/:username.
Zend_Controller_Router
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.example.com',
array(
'controller' => 'profile',
'action' => 'users'
)
);
$plainPathRoute = new Zend_Controller_Router_Route_Static('');
$router->addRoute('user', $hostnameRoute->chain($plainPathRoute));
Examples Documentation : Zend_Controller_Router

Sounds like you're after hostname routes: http://framework.zend.com/manual/en/zend.controller.router.html#zend.controller.router.routes.hostname

You can use Routes in Zend (http://framework.zend.com/manual/en/zend.controller.router.html) and then mod_rewrite + .htacess for apache (http://httpd.apache.org/docs/current/mod/mod_rewrite.html) or the URL rewrite module in IIS (http://www.iis.net/download/URLRewrite)
These functions are "needed" for using Zend at all, as the URL mysite.com/demo1/demo2/ actually translates to something like mysite.com/index.php?controller=demo1?action=demo2

Related

How to get the variables in routing in zend framework 1.12

I am new to routing in zf. I don't understand some terms in route
$route = new Zend_Controller_Router_Route(
'author/:username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
How do we get :username here? from where do we get this?
Username here is parameter pass by the user. So correct link using this route will be http://somepage.com/author/John. In your controller you can get this variable just like POST and GET variables - $this->getParam('author');
If you want to allow user use link without parameter (default parameter) you can add to array - 'author' => null (i usually use this in pagination)

Zend Framework Routing Technique

I would like to know if this method of setting up routing in Zend Framework would be considered a reasonable approach - I am quite new to using ZF.
In my bootstrap file I have a method set up like this:
protected function _initRouting()
{
$zfc = Zend_Controller_Front::getInstance();
$router = $zfc->getRouter();
$router->addRoute(
'home',
new Zend_Controller_Router_Route(
'',
array(
'controller' => 'index',
'action' => 'index'
)
)
);
$router->addRoute(
'saveStory',
new Zend_Controller_Router_Route(
'save-story/:date/:seolink/:saveStory',
array(
'controller' => 'story',
'action' => 'index',
'saveStory' => 1
)
)
);
etc etc with all my routes.
This works fine and I like the clarity of specifying routes like this but have a nagging feeling that more experienced ZF programmers would tell me that it is not the best way and that I should not do it in the bootstrap and that I should specify the routes in a seperate config file. If this is the case what form would the config file take and how and where would I read it.
Any advice would be appreciated....Because my technique works this question is more a stylistic question - what is the most 'elegant' approach.....Thanks
It's just down to personal preference. Like you, I prefer to define my routes in code, but some people prefer config files (and if you want to try this there are several examples in the manual).

Pretty Zend Framework urls

I would like to have my urls like this:
/index
/contact
/articles
/articles/selection
...
Instead of:
/index/index
/index/contact
/articles/index
/articles/selection
...
Basically I have only one controller. Which solution is the best to perform this? (controllers and redirections, ZF routing, url rewriting, something else?)
Have a look at the documentation. The behaviour you want is configured as default in the default router:
http://framework.zend.com/manual/en/zend.controller.router.html
if the first param do not maps a module name, it will search for a controller and if this fails too, it is looking for an action in your IndexController.
Did you tried calling your url's like you want to?
What happens if you navigate to /index? Should be the same like /index/index
use zend routing :
$router = Zend_Controller_Front::getInstance()->getRouter();
$route_index = new Zend_Controller_Router_Route(':action', array(
'module' => 'default',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('route_index', $route_index );
$route_articles = new Zend_Controller_Router_Route('articles/:action', array(
'module' => 'default',
'controller' => 'articles',
'action' => 'index'
));
$router->addRoute('route_articles ', $route_articles );

What is Route, where and how to define it?

I have several modules in my zend application. On one of the view script of my modules, I created a URL as such
$links['create'] = $this -> url(array("controller" => "roles", "action" => "create"), "custom");
This brings an error, saying Route "custom" is not define.
What is Route? Where to define it and How?
In my bootstrap file i have initialized my routing by adding following function
public function _initRouting() {
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$routedetialevent = new Zend_Controller_Router_Route(
'/events/detail/:id',
array(
'controller' => 'events',
'action' => 'detail'
)
);
$routeregister = new Zend_Controller_Router_Route(
'/index/register/:id',
array(
'controller' => 'index',
'action' => 'register'
)
);
$routerdetail = new Zend_Controller_Router_Route(
'/commentaries/details/:id',
array(
'controller' => 'commentaries',
'action' => 'details'
)
);
$router->addRoute('post', $routedetialevent);
$router->addRoute('register', $routeregister);
$router->addRoute('detail', $routerdetail);
}
as i have added the custom route in my events, commentaries whenever i visit detail page i dont have to write id in my url so my url will be like
http://localhost/example/events/detail/3
If i wouldnt have added route than my url would be like
http://localhost/example/events/detail/id/3
The Zend Framework manual has pretty decent documentation about routes and the router, including descriptions of several ways to define routes.
At a very basic level, routes are used both to parse URLs into parameters (like which controller and action should be used), and to do the reverse: take parameters and produce a URL.
For your purposes, unless you want to change how ZF will build your URL, you can just drop the "custom" part off of your url call.

Zend Framework Router Getting /module/VALUE/controller/action

I've been googling around and I can't seem to find anything which explains the use of ZF router well. I've read the documentation on the site, which seems to only talk about re-routing.
I am trying to make the format:
/module/value/controller/action give /module/controller/action passing on value as a parameter
e.g.
/store/johnsmithbigsale/home/newstuff would route to /store/home/newstuff passing on johnsmithbigsale as the value to a parameter with a hidden namespace e.g. storeName.
Some help would be greatful!
You can use Zend_Controller_Router_Route to map your url parts to modules, controllers, actions, and parameters that can be used in the controller by $this->_getParam('varName'). You can define these routes in the application.ini file or in the application bootstrap.
// custom city route
$route = new Zend_Controller_Router_Route(
'cities/:city',
array(
'controller' => 'city',
'action' => 'view'
)
);
$this->addRoute('city', $route);
// custom buy widgets route
$route = new Zend_Controller_Router_Route_Regex(
'buy_(.+)_widgets/([0-9]+)(.*)',
array(
'controller' => 'widgets',
'action' => 'view'
),
array(
1 => 'nothing',
2 => 'widget_id',
3 => 'vars'
)
);
$this->addRoute('widgets', $route);
The regex route is kind of specific to my app, but you can see that each match can get mapped to a parameter.