I'm using Zend, and here is my problem, i have two different urls that i really want to keep like they are.
i want url : "www.urlA.com" to be directed to application/moduleA/indexController/indexAction
and "www.urlB.com" to application/index/index.
In other words, i want Zend_Router to make sure that when i type www.urlA.com/index/login i use the application/moduleA/ Index controller and loginAction().
I want to keep the classic Zend routing, just adding the fact that my module is already specified in the url.
I have the following code int the bootstrap:
protected function _initRouter()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route_Hostname(
'www.urlA.com',
array(
'module'=>'moduleA'
)
);
$routeURI = new Zend_Controller_Router_Route();
$router->addRoute('modulea', $route->chain($routeURI));
}
This way with the "urlA" i correctly go to moduleA/index/index but
"urlA/index/login" doesn't work.
Thanks for any help.
I had a similar problem once and I wrote this :
//ADMIN page
$admin = array('module' => 'admin', 'controller' => 'index', 'action' => 'index');
$hostRoute_admin = new Zend_Controller_Router_Route_Hostname('admin.mysite.com', $admin);
//special environement Website
$env = array('module' => 'env', 'controller' => 'index', 'action' => 'index');
$hostRoute_env = new Zend_Controller_Router_Route_Hostname('env.mysite.com', $env);
//Zend classic routing
$plainPathRoute = new Zend_Controller_Router_Route(':controller/:action/*',
array('controller' => 'index', 'action' => 'index'));
//add specific routing
Zend_Controller_Front::getInstance()->getRouter()->addRoute('admin', $hostRoute_admin->chain($plainPathRoute));
Zend_Controller_Front::getInstance()->getRouter()->addRoute('env', $hostRoute_env->chain($plainPathRoute));
Related
I am using CakePHP 1.3 and have some troubles with prefix routing.
I configured routes like that:
Router::connect(
'/listing/*',
array(
'controller' => 'dsc_dates',
'action' => 'listing',
)
);
Router::connect(
'/modular/listing/*',
array(
'controller' => 'dsc_dates',
'action' => 'listing',
'prefix' => 'modular'
)
);
in my controller there are two functions:
function modular_listing($order = null,$orderDirection = null, $items=null, $location_id=null) {
$this->layout='module';
$this->setAction('listing',$order, $orderDirection, $items, $location_id);
}
function listing($order = null,$orderDirection = null, $items=null, $location_id=null){...}
The prefix action should just change some things and then operate like the normal 'listing' method. Until here it works fine.
But if i create relative links (with HTML Helper) Router::url() uses 'modular_listing' as action which does not fit into my routes. It should be 'listing' instead of 'modular_listing'.
The controller params are correct with 'listing' as action but the router params still says 'modular_listing'.
So relative links:
$this->Html->link('example',array('parameter'));
will end up in:
/dsc_dates/modular_listing/parameter
How can I get the correct links so that the router uses 'listing' as action?
UPDATE:
It is not an alternative to add 'controller' and 'action' to the url array of the link generation. In fact I have problems with the automatically generated relative links from the paginator.
I couldn't tell if you wanted the generated Html->link() routes with the leading controller or not, so I did both:
Controller (note the renderer):
// DscDatesController.php
public function listing($param = null) {
$this->set('param', $param);
$this->render('listing');
}
public function modular_listing($param = null) {
//
$this->setAction('listing', $param);
}
Routes:
// routes.php
Router::connect(
// notice no leading DS
'listing/*',
array(
'controller' => 'DscDates',
'action' => 'listing'
)
);
Router::connect(
'/modular/listing/*',
array(
'controller' => 'DscDates',
'action' => 'listing'
)
);
View:
// DscDates/listing.ctp
<?php
// generates /dsc_dates/listing/:param
echo $this->Html->link(
'example',
array('controller'=>'dsc_dates', 'action'=>'listing', $param));
// generates /listing/:param
echo $this->Html->link(
'example',
array('action'=>'listing', $param));
About wildcards, DS and routing order:
CakePHP broken index method
HTH :)
I have written the following code in bootstrap.php file
protected function _initRoutes() {
$routers = Zend_Controller_Front::getInstance()->getRouter();
$adminadd = new Zend_Controller_Router_Route('/:cityadd/', array('module' => 'user', 'controller' => 'city', 'action' => 'add'));
$routers->addRoute('addcity', $adminadd);
$routing = Zend_Controller_Front::getInstance()->getRouter();
$adminedit = new Zend_Controller_Router_Route('/:cityedit/', array('module' => 'user', 'controller' => 'city', 'action' => 'edit'));
$routing->addRoute('edit-city', $adminedit);
}
My project name is demo
In my browser when I give the URL http://localhost/demo/public/cityadd the page opened is add action page i.e,
View script for controller City and script/action name add
When I give the URL http://localhost/demo/public/cityedit also the page opened is add action page i.e,
View script for controller City and script/action name add
instead it must be redirected to View script for controller City and script/action name edit
Why the same page is opened or why the page is redirected to same action for any URL given
The problem is that you're using variables in your routes. Variables are preceded with a colon. A route consisting only of a variable will match pretty much anything.
Try writing your routes without the colon:
protected function _initRoutes()
{
$routers = Zend_Controller_Front::getInstance()->getRouter();
$adminadd = new Zend_Controller_Router_Route('/cityadd/', array('module' => 'user', 'controller' => 'city', 'action' => 'add'));
$routers->addRoute('addcity', $adminadd);
$routing = Zend_Controller_Front::getInstance()->getRouter();
$adminedit = new Zend_Controller_Router_Route('/cityedit/', array('module' => 'user', 'controller' => 'city', 'action' => 'edit'));
$routing->addRoute('edit-city', $adminedit);
}
Is it possible to have pages route without the controller in the URL but still have other controllers work? Example:
Access pages like this: http://domain.com/about/
Instead of like this: http://domain.com/pages/about/
But still have access to http://domain.com/othercontroller/action/
Doing the following works for having the pages without /pages/ in the URL but if I try to access any other controller it doesn't work:
From: Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
To: Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'index'));
Is there a way to setup the Router so that it runs controller/action if it exists. If it does not it runs the pages controller/action?
I think the short answer is, no - it's not possible in the manner you're hoping*. Routing isn't really "logic" driven, so unless you can come up with a way to match the things you want in both respects you can't do "if controller exists, then _, else _" kind of thing.
*You could, however add each "page" as a row in your routes file. That would allow "about", "contact" ...etc to be accessed directly, while things that don't exactly match them are handled by the remaining routes.
I know I'm late, but here's my tip for someone looking for this.
In routes.php
foreach(scandir('../View/Pages') as $path){
if(pathinfo($path, PATHINFO_EXTENSION) == "ctp"){
$name = pathinfo($path, PATHINFO_FILENAME);
Router::connect('/'.$name, array('controller' => 'pages', 'action' => 'display', $name));
}
}
This will create a route for every ctp file in the View/Pages folder.
I actually solved this the opposite way from Dave's answer above, by adding a route for each controller, rather than each page. (I won't be adding new controllers very often, but I will be adding new content on a regular basis.)
// define an array of all controllers that I want to be able to view the index page of
$indexControllers = array('posts','events','users');
//create a route for each controller's index view
foreach ($indexControllers as $controller) {
Router::connect(
'/' . $controller,
array(
'controller' => $controller,
'action' => 'index'
)
);
}
//create a route to remove 'view' from all page URLs
Router::connect(
'/:title',
array(
'controller' => 'contents',
'action' => 'view'
),
array(
'pass' => array('title'),
'title' => '[a-z0-9_\-]*'
)
);
i am building a web service with zend and i am using modules to separate my api versions. Ex: "applications/modules/v1/controllers", "applications/modules/v2/controllers" have different set of actions and functionality.
I have made "v1" as the default module in "application.ini" file:
resources.modules = ""
resources.frontController.defaultModule = "v1"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.frontController.moduleControllerDirectoryName = "controllers"
I have written the following in my bootstrap file:
$router = $front->getRouter();
$r1 = new Zend_Controller_Router_Route_Regex('api/v1/tags.xml',
array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'));
$router->addRoute('route1', $r1);
Suppose, if this is my url: http://localhost/api/v1/tags.xml
then it belongs to version 1 (v1).
But i dont want to write many routes like this one, so i want to know how can i track the version from the regex url and dynamically determine the api version to be used (1 or 2).
try to use
$r1->addRoute(
'json_request',
new Zend_Controller_Router_Route_Regex(
'([^-]*)/([^-]*)/([^-]*)\.xml',
array(
'controller' => 'index',
'action' => 'index',
'request_type' => 'xml'),
array(
1 => 'module',
2 => 'controller',
3 => 'action'
)
));
Try this:
$r1 = new Zend_Controller_Router_Route_Regex('api/(v.*)/tags.xml',
array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'),
array(1 => 'module')
);
This will automatically overwrite the module param, and should therefor automatically route to the right module. No need to use a plug-in with the preDispatch method anymore.
So far, i tried like this:
$r1 = new Zend_Controller_Router_Route_Regex('api/v(.*)/tags.xml',
array('module' => 'v1', 'controller' => 'tags', 'action' => 'index'),
array(1 => 'version')
);
$router->addRoute('route1', $r1);
And I could get an idea from here:
So now i used a front controller and in preDispatch method, i am setting the module name based on the value i get in the "version" parameter value, like
if($request->getParam('version') == 2 { $request->setModuleName('v2') }
But after changing the version in url to v2, it still goes to the action of controller in v1 module.
I am trying to connect /admin/ to a static page 'admin.ctp'.
I copied the pages controller for modification and copied the display function to admin_display. I also tried creating an admin_index function without parameters. My route looks like this at this moment:
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin'));
my admin_index function looks like this:
function admin_index() {
$page = 'admin';
$subpage = null;
$title_for_layout = 'Admin';
$this->set(compact('page', 'subpage', 'title_for_layout'));
$this->render('/admin');
}
I put admin.ctp in /views/pages/ and in /views/pages/admin/
Anyway. When I go to /admin/ it redirects me to /. But when I delete admin_index, it complains that the function does not exist, so I does look for it.
Help?
edit: Big correction, all my admin urls go back to /
edit2: resolved it, something with appcontroller :$
Create admin_index.ctp file in /views/pages/.
Remove $this->render('/admin'); from the admin_index function. (If you wanted to use admin.ctp, I think all you would have to do is to remove the / from the argument). There's no reason to render admin.ctp for admin_index, since it's natural for cake to render admin_index.ctp for admin_index function. You just don't gain anything by not doing that the cake way.
If it doesn't work, try
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'prefix' => 'admin', 'admin' => true));
If you want to route /admin/*action* requests to pages controllers admin_action function, then add this line to routes.php:
Router::connect('/admin/:action/*', array('controller' => 'pages', 'prefix' => 'admin', 'admin' => true));
Router::connect('/admin/', array('controller' => 'pages', 'action' => 'index', 'admin')); would work with the standard pages controller