I need to Add the route to Zend Framwork Application.
I want to display All the Posts from the database where catagory = the controller name
domain.com/action
domian.com/drama
domain.com/thriller
How can i do this ? I gone through the ZF Routes Documentation. But found No Solution.
To be able to do this, you will have to put something like this in your application.ini
resources.router.routes.category.type = "Zend_Controller_Router_Route"
resources.router.routes.category.route = ":category"
resources.router.routes.category.defaults.module = "default"
resources.router.routes.category.defaults.controller = "index"
resources.router.routes.category.defaults.action = "index"
This way, everything that didn't match as a valid controller will be directed as category parameter in index controller index action. Keep in mind to handle invalid category names and trigger 404.
Also, here's a good article about tricks and tips in application.ini
This can be done using addRoute() method in the bootstrap.php file
// Retrieve the front controller from the bootstrap registry
$FrontController = $this->getResource('FrontController');
$router = $FrontController->getRouter();
$router->addRoute('genre',
new Zend_Controller_Router_Route(
':genre',array( 'controller' => 'index' , 'action' => 'index' )) );
To get the Genre in the Controller
echo $this->getRequest()->getParam('genre')
Related
I am using Zend Framework version 1.12.16. I have created two modules i.e. user and admin. I have set user module as my default module by enabling it in application.ini:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resouces.modules[]=
resources.frontController.defaultModule = "user"
So, whenever users the name of the site in the url he is automatically taken to index action of indexController in user module. Similarly, I want the user to be taken to index action of the loginController of admin module whenever user types in http://example.local/admin. Can I achieve that using some settings in application.ini ?
Zend only allows for a single definition of default module/controller/action. Behind the scenes, this creates a default route in Zend's routing system for you. Routing is the key for your problem. To match your case, you could of course do some dynamic/placeholder/whatever overkill, but a simple static route is all you need actually. This can look like that:
$route = new Zend_Controller_Router_Route_Static(
'admin',
array('module' => 'admin', 'controller' => 'login', 'action' => 'index')
);
$router->addRoute('admin', $route);
Thanks Jossif!! I completely forgot this option. Adding the following method in Bootstrap.php class resolved my problem.
protected function _initRoutes()
{
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route_Static(
'admin',
array(
'module' => 'admin',
'controller' => 'login',
'action' => 'index'
)
);
$router->addRoute('admin', $route);
}
I'm trying to define RESTful routes for sub directory controllers. I want to be able to create routes for the url at admin/questions/*. My controller is Admin_QuestionsController:
- application
- controllers
-Admin
QuestionsController.php (class Admin_QuestionsController)
Below is how I'm declaring my RESTful route for this controller:
$restRoute = new Zend_Rest_Route($front, array(), array(
'admin' => array('questions')
));
$router->addRoute('rest', $restRoute);
..from the documentation I can't see what I'm doing wrong - http://framework.zend.com/manual/1.12/en/zend.controller.router.html#zend.controller.router.routes.rest. However, I get the following error:
Invalid controller specified (admin)
I can get the routes to work when I declare then not as Rest routes:
$router->addRoute('admin_questions',
new Zend_Controller_Router_Route( '/admin/questions', array(
'controller' => 'Admin_Questions',
'action' => 'index')
)
);
..so I don't think I've have the folder structure wrong, or the class name. But, I need RESTful routes which I'm unable to achieve.
The Zend_Rest_Route route like you have defined, works if you have Zend modules enabled. The documentation mentions "translating the HTTP method and the URI to a module, controller and action". To enable modules, add the following two lines in your application.ini:
resources.modules[] =
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
Then create a directory in application/modules named admin/controllers, and create your QuestionsController in application/modules/admin/controllers/QuestionsController.php.
The rest of your application should (hopefully) still work as the default module.
In my project I have some pages defined in my index controller, like ie. the About us page. In order for me not to have to type domain.com/index/about but domain.com/about I have this route:
$route = new Zend_Controller_Router_Route_Static ( 'about', array (
'controller' => 'Index',
'action' => 'about'
) );
$router->addRoute ( 'about', $route );
It does the job. The problem is that sometimes I have 6 or 7 pages and I have to repeat this route 6 or 7 times. Is there a way for me to do a route that would always remove "index" from the url? I would never need a url that has index in it. Thanks!
You can write dynamic routes by avoiding the static route type:
$route = new Zend_Controller_Router_Route(
'/:action',
array (
'controller' => 'index',
'action' => 'index',
)
);
$router->addRoute('pages', $route);
This will add a route called 'pages' that will match any single action in the index controller. The action and controller defined in this route are merely the defaults and as you are not passing the controller as a variable it will always route to the IndexController. The action will default to indexAction but can be overridden by the route, ie:
/about -> IndexController / aboutAction
/contact -> IndexController / contactAction
etc ...
Bear in mind this will override any other routes so you need to structure your routing heirachy properly. Routes defined later in the process will override routes already defined.
For more information check the docs: Zend Framework Standard Router
I understand how to create a simple custom route in the form of
example.com/archive/:year
However, I need to create a route in the form of
example.com/:make/:model
Both path components are variables. At the same time I want to keep the default Zend Framework routing. How would I go about this?
Thanks
Not for zend, but the technique is instructive and will probably work. I've struggled with this problem too, but was more for internationalisation.
http://darrenonthe.net/2011/05/06/dynamic-routing-from-database-in-codeigniter/
Basically, you cache your routes into a file, and the framework matches them to your controller/variable details. I tried lots of things, complex regexes, and in the end, this worked out really, really well. Might be a good solution for you too.
if you are using a module based file architecture, you can maintain the zend framework default routes, and add custom routes for your modules.
for example
class Example_Bootstrap extends Zend_Application_Module_Bootstrap
{
public function _initModuleRoutes()
{
$this->bootstrap('FrontController');
$frontController = $this->getResource('FrontController');
$router = $frontController->getRouter();
$route = new Zend_Controller_Router_Route(
'modulename/:action/*',
array(
'module' => 'modulename',
'controller' => 'modulecontroller',
'action' => 'index'
)
);
$router->addRoute('routename', $route);
return $router;
}
You need to enforce some condition i.e model is integer type or something else . Like this
$route = new Zend_Controller_Router_Route(
':make/:model',
array(
'controller' => 'car',
'action' => 'logic'
),
array('make' => '\d+')
);
If you cannot distinguish them with extra condition like these then how software gone do this for you weather its action name or make ?
I want to make a kind of alias for my modules:
Is there a way to make
http://www.example.com/news point to
/modules/news/controller/news/->action
index.
And if i go to
http://www.example.com/news/show it
automaticly points to
/modules/news/controller/news/ ->
action show
Or do I have to make a route for every single action I make in this module?
If I don't make a route my links will look like:
http://www.example.com/news/news/show and http://www.example.com/news/news/show.
P.s i'm using Zend 1.10.6
Thanx in advance!
I guess it will be something like that:
$route = new Zend_Controller_Router_Route(
'news',
array('module' => 'news', 'controller' => 'news', 'action' => 'index'));
$router->addRoute('news', $route);
where first arg 'news' for Zend_Controller_Router_Route is the url http://www.example.com/news that will route to http://www.example.com/news/news/index
salu2
In your config file you must enable modules by putting this in your config file
resources.modules[] =
And it should by default route correctly in your application.
You will have to define a route for each URL you want routed in a non-standard way.