How to rewrite domain in Zend Framework - zend-framework

I am writing a user registration form.
After the user registers with a username e.g. "Billy", I would like him to use the url billy.mydomain.com to access the server.
The Zend framework link of billy.mydomain.com is
www.mydomain.com/profile/index/username/billy
I would like to know how I can rewrite the url from
www.mydomain.com/profile/index/username/billy
to
billy.mydomain.com

Throwing away my initial answer, This Solution is much more elegant.
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.mydomain.com',
array(
'controller' => 'profile',
)
);
$pathRoute = new Zend_Controller_Router_Route(":action/*", array( 'action'=>'index' ));
$chain = $hostnameRoute->chain($pathRoute);
// add $chain to your router

Related

Route sudmains controller with Zend

I have the following structure on my app:
Modules =>
default => site.com
blog => blog.site.com
admin => admin.site.com
I used this code on my bootstrap to allow subdomains and redirect to the follow module:
$pathRoute = new Zend_Controller_Router_Route(':controller/:action/*', array('controller' => 'index', 'action' => 'index'));
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$blogDomainRoute = new Zend_Controller_Router_Route_Hostname(
'blog.site.com', array(
'module' => 'blog',
'controller' => 'index',
'action' => 'index'
));
$router->addRoute('blogdomain', $blogDomainRoute->chain($pathRoute));
And the same code to adminDomainRoute.
It works fine! But now i notice that my pagination route don't work, i have the follow route to manage pages in admin module:
routes.usuarios.route = /usuarios/pagina/:pagina
routes.usuarios.defaults.module = admin
routes.usuarios.defaults.controller = usuarios
routes.usuarios.defaults.action = index
routes.usuarios.defaults.pagina = 1
I tried to change the route to
routes.usuarios.route = admin.site.com/usuarios/pagina/:pagina
But i still got action no found:
array (
'controller' => 'usuarios',
'action' => 'pagina',
'module' => 'admin',
)
How can i route admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3 ?
The thing that jumps at me from your setup is, that in the ini format (your current admin route), you are using the default router. Well this router knows nothing about the hostname you are on, so it is looking for an url like this one:
site.com/admin.site.com/usuarios/pagina/1 admin.site.com/usuarios/pagina/3
What you want is something like this:
//Create a hostname route. This route is only concerned with the subdomain part of the uri
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
'admin.:host.:domain');
//Create a default router that would take care of the rest of the routing.
$defaultRoute = new Zend_Controller_Router_Route(
'/usuarios/pagina/:pagina',
array(
'module'=>'admin',
'controller'=>'usuarios',
'action'=>'index'
)
);
//Chain those two routes together to make them go one after the other.
$defaultRoute=$hostnameRoute->chain($defaultRoute);
This code may need a bit of tweaking, but I think this should do what you need.

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).

Zend View: Domain to Action

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

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.

Need to even further shorten easy urls using Zend Route

I might be asking a bit too much here but I would like to know how can I further shorten urls such that they look like there's not an inclusion of a controller.
eg: I want to shorten this:
www.mysite.com/users/Bob-123
to
www.mysite.com/Bob-123
or something like www.mysite.com/bob-123-user
Any ideas here? - I'm using the Zend framework here
You can do with the help of Zend Router .
Here you need to fetch the usernames and add routes to it. This way it will recognise which controller and action it needs to fetch.
$router = $this->_front->getRouter();
$route = new Zend_Controller_Router_Route(
':username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
An example taken from zend manual
In your application.ini you could do it using Zend_Controller_Router_Route_Regex:
resources.router.routes.user.route = "(\w+)-(\d+)"
resources.router.routes.user.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.user.defaults.controller = users
resources.router.routes.user.defaults.action = index
resources.router.routes.user.map.1 = username
resources.router.routes.user.map.2 = id
resources.router.routes.user.reverse = "/%s-%d"
You would need to specify your own action and controller.
Hope this helps.