Hostname and Custom routing in Zend Framework don't work together for me - zend-framework

I am building an application that uses hostname routing to detect subdomains like
user1.example.com
user2.example.com
and also have custom routes like user1.example.com/login
This works well so far, however when I add custom routes they do not work. I have searched and read a lot but seems there is something I am missing. Here is what I have so far:
//my routes in routes.ini
[development]
routes.login.type = "Zend_Controller_Router_Route"
routes.login.route = "/login"
routes.login.defaults.controller = "user"
routes.login.defaults.action = "login"
//This part in Bootstrap file
$this->bootstrap('frontController');
$router = $this->frontController->getRouter();
$routerConfig = new Zend_Config_Ini(
APPLICATION_PATH . '/configs/routes.ini',
'production'
);
//I create a default route
$routeDefault = new Zend_Controller_Router_Route_Module(
array(),
$this->frontController->getDispatcher(),
$this->frontController->getRequest()
);
$router->addConfig($routerConfig, 'routes');
// hostname route
$hostnameRoute = new Zend_Controller_Router_Route_Hostname(
':username.mysite.com',
array(
'module' => 'default',
'controller' => 'index',
'action' => 'index',
)
);
//I add the default route.
$router->addRoute('default', $routeDefault);
//I chain the routes so that all routes have subdomain routing too
foreach ($router->getRoutes() as $key => $theroute) {
$router->addRoute($key, $hostnameRoute->chain($theroute));
}
When I go to a custom route like http://user1.example.com/login I get the error: 'Invalid controller specified (login)' which means my custom route is not being recognized. I am also not sure if the way I am adding the default route is correct and necessary. If I remove that code then it doesn't work. So my problem really is that I would like my hostname matching, custom routes and default routes to all work. If you can spot where I'm going wrong please help, I have read previous related posts all over on routes, chaining, default routes etc (including this very related one: How do I write Routing Chains for a Subdomain in Zend Framework in a routing INI file?) but haven't found the solution so far.

You should be able to setup your routing using a custom param in the route:
routes.testdynamicsubdomain.type = "Zend_Controller_Router_Route_Hostname"
routes.testdynamicsubdomain.route = ":subdomain.domain.dev"
routes.testdynamicsubdomain.defaults.module = public
routes.testdynamicsubdomain.defaults.controller = index
routes.testdynamicsubdomain.defaults.action = index
If your apache/hostfile etc are configured correctly going to test.domain.dev should load the index action in your indexController where you could get the :subdomain param:
echo $this->getRequest()->getParam('subdomain');
Also, as you discovered, the order of the routes is very important. See also Zend Router precedence for more info about this.

Related

how to make optional parametres in zendframework URL

I am new to Zend, but very very keen to learn. This is really just a quick question on routing in Zend Framework.
I understand the basic of it but I am still confused about how I can create some optional parameters at the end of my URL. For example, I have the following default page URL:
examplesite.com/accounts/enquiry
I now want to add two additional parameters to it i.e:
userid= 6
location= 12
So, the eventual URL should look like:
examplesite.com/accounts/enquiry/6/12
but
examplesite.com/accounts/enquiry
Will get you to the same page.
I am not clear. How do it do this? I mean, this is not a bespoke URL. so, I don't need to create a custom route. It basically just the last two parameters that need to be added to the page.
How do I do this?
First 2 parameters are controller and action name, the named params.
Here you are:
examplesite.com/accounts/enquiry/userid/6/location/12
or you can define your own route like this:
$route = new Zend_Controller_Router_Route('accounts/enquiry/:userid/:location);
and then add it to router:
$router->addRoute('accounts', $route);
You could add a custom route inside your Bootstrap.php, e.g. (untested):
protected function _initRoutes()
{
[...]
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$accounts = new Zend_Controller_Router_Route(
'accounts/enquiry/:userid/:location',
array(
'userid' => '[0-9]{2}',
'location' => '[0-9]{2}',
'controller' => 'accounts',
'action' => 'enquiry',
)
);
$router->addRoute('accounts', $accounts);
[...]
}
http://framework.zend.com/manual/1.12/en/zend.controller.router.html

hide the index controller from the URL for a single module

After creating a modular structure for a single module would prevent the url appears the name of the controller.
everything works with the defaul
site.con/foo/index/action/
I wish I could write as
site.com/foo/action/
being IndexController the only controller that module.
I have tested several solutions but do not work. Being the first app with ZF I do not quite clear the steps to be taken.
You need Zend Routes.
Define routes in your
bootstrap.php
Open your bootstrap.php and put the following:
function _initRoutes() {
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$router->addRoute('foo-action', new Zend_Controller_Router_Route(
'<foo module name>/<action name>', array('module' => 'foo', 'controller' => 'index', 'action' => '<action-name>')
));
}
PS:Worked / Didn't work?
Mention in comments and if didn't work, give proper names of module, controller and action.
EDIT:
How to set default controller / module in application.ini
routes.index.type = "Zend_Controller_Router_Route"
routes.index.route = "/"
routes.index.defaults.module = "<module name>"
routes.index.defaults.controller = "index"
routes.index.defaults.action = "index"
Solves it?

Zend Route Regex and invalid controller

php i have method to add routes:
public function addRoutes()
{
$front = Zend_Controller_Front::getInstance();
$redirect = $front->getRouter();
$router = new Zend_Controller_Router_Route_Regex(
"p\/(a-zA-Z0-9)\.htm",
array(
'controller'=>'page',
'action'=>'index',
1=>'ja.htm'
),
array( 1 => 'page_name')
);
$route2 = new Zend_Controller_Router_Route_Regex("(a-zA-Z0-9)\.html",
array('controller'=>'page',
'action'=>'index',
1=>'ja.html'),
array(1=>'page_name'));
$redirect->addRoute('pages',$router);
$redirect->addRoute('hmtmled',$route2);
$front->setRouter($redirect);
}
I tried to enter url like: p/ja.htm but i get error: Invalid controller specified (p). I know its for reason of default route, but how to change that?
Is that method part of your Bootstrap class? If so, are you sure it is being run? Remember, the Bootstrap methods that get called automatically are those of the form _initXXX() (note the leading underscore).
Also, as #Tim Fountain astutely notes in the comments, the regex needs to be:
p/([0-9A-Za-z]+)\.htm
you try to remove the default routes:
//excerpt from ZF reference 24.5.4. Default Routes... If you do
not want this particular default route in your routing schema, you may
override it by creating your own 'default' route (i.e., storing it
under the name of 'default') or removing it altogether by using
removeDefaultRoutes():
// Remove any default routes
$router->removeDefaultRoutes();

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.

How to route multi subdomain with zend router hostname

I need to create routing in Zend to simply copy the current live site url structure which is sadly inconsistent
What i want to do is to route subdomain as follow:
www.site.com -> static router
a.site.com & b.site.com -> category controller
c.site.com & d.site.com -> location controller
the rest sub domain -> user controller
could anyone guide me how to solve this, thanks.
UPDATE:
First thanks Fge, vote your answer, it works but i need some more advice:
Since i have many subdomains for each rules is there a better way than add the rules in looping
foreach($subdomains as $a){
$tr = new Zend_Controller_Router_Route_Hostname(
"$a.site.com",
array(
'module' => 'mod',
'controller' => 'ctrl',
'param_1' => $a
));
$router->addRoute($a,$tr);
}
How to combine it with other routing type to parse the parameters (chained?), something like http://a.site.com/:b/:c, i want t parse it to param_1 (a), param_2 (b), param_2 (c)
Note: Reverse Matching
Routes are
matched in reverse order so make sure
your most generic routes are defined
first.
(Zend_Controller_Router)
Thus you have to define the route for all other subdomains first, then the specific ones:
$user = new Zend_Controller_Router_Route_Hostname(
':subdomain.site.com',
array(
'controller' => 'user'
)
);
$location1 = new Zend_Controller_Router_Route_Hostname(
'c.site.com',
array(
'controller' => 'location'
)
);
$location1 = new Zend_Controller_Router_Route_Hostname(
'd.site.com',
array(
'controller' => 'location'
)
);
// other definitions with known subdomain
$router->addRoute($user); // most general one added first
$router->addRoute($location1);
$router->addRoute($location2);
// add all other subdomains
Update for the updated question:
1) This really depends on how different the parameters are you want to route a subdomain to. In your example you routed them all to the same model and controller and added the actual subdomain as a parameter. This can be done easily with the user-route i posted above. There the subdomain is set as parameter subdomain ($request->getParam("subdomain")). If you want the subdomains to be the action of a known controller/model you could replace :subdomain with :action. But as soon as you have other controllers/models for each subdomain, I'm affraid you have to loop over them (or use a config file). For the example you provided in the question, the route simply could look like this:
$user = new Zend_Controller_Router_Route_Hostname(
':param1.site.com',
array(
'controller' => 'user'
)
);
// routes "subdomain".site.com to defaultModul/userController/indexAction with additional parameter param1 => subdomain.
As long as you don't have any schema in your subdomains it's very difficult to route them in a general way.
2) That's an example where router chains come into play. The outer route would be the hostname route which handles the subdomain and the inner route would handle the :a/:b part. This could look like this for example:
$user->chain(new Zend_Controller_Router_Route(':a/:b'));