Zend Framework - module routing - zend-framework

I have a module called users. Inside I have an index controller, with an action called part1Action() inside the index controller class.
I expected to be able to access the part1Action() action through the path /user/index/part1, but I'm ending up in the /user/index/index action.
I have other modules set up that are working from a module/controller point of view, but I've not tried accessing other actions apart from the index action in these, so this is the first time I've tried routing to other than the index action.
More info, I have this in my application.ini:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules = ""
autoloaderNamespaces[] = "Users_"
Where am I going wrong?

part1Action is not going to be a valid Action name as it contains a number. Name the action something like partOneAction. If you absolutely want to use a number in your url set up a routing rule like so
$front->getRouter()->addRoute(
'part-1',
new Zend_Controller_Router_Route_Static(
'/user/index/part1/',
array('module' => 'users', 'controller' => 'index', 'action' => 'part-one')
)
);
Honestly though, I would probably set my module up a little differently. I'm assuming that this is for a multi-page form of some kind?
so:
modules
user
controllers
{some-meaningful-name}Controller.php
inside of {some-meaningful-controller-name}Controller.php
public function {some-meaningful-action-name}Action()
{
$part = $this->getRequest()->getParam('part', 1);
}
that way your url would be /user/{some-meaningful-controller-name}/{some-meaningful-action-name}/part/{some-number}

Related

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 Module Bootstrap does not load

I have a very strange case where my Module is working but my Module's boostrap is not being loaded.
Here is the segment in my application.ini for module autoloading:
resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
resources.modules[] = ""
Here is the bootstrapper:
protected function _initAutoload()
{
$autoloader = new Zend_Application_Module_Autoloader(array(
'namespace' => 'User_',
'basePath' => APPLICATION_PATH .'/modules/user',
'resourceTypes' => array (
'model' => array(
'path' => 'models',
'namespace' => 'Model',
)
)
));
}
Structure of my modules
Application
--modules
----user
------config/
------controllers/
------models/
------views/
------Bootstrap.php
----admin
The problem here is that User_Bootstrap is not being loaded.
<?php
class User_Bootstrap extends Zend_Application_Module_Bootstrap
{
protected function _initAutoload()
{
Zend_Registry::set('debug', 'haha');
}
}
By doing a Zend_Registry::get('debug') on any controller, it doesn't recognize that the key was set in the module bootstrap. In fact any syntax error in the User_Bootstrap does not work.
I don't know why User_Bootstrap is not being autoloaded. This is driving me crazy because I've been researching for 5 hours and can't even get a blog post close to covering this case...
Speaking of which, my models and controller classes are being autoloaded fine.
Try the following...
Change your application.ini file to use
; lose the quotes
resources.modules[] =
See http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.modules
Remove the _initAutoload() method from your Application Bootstrap class. You don't need this as the module bootstrap will automatically create a resource loader for your User_ classes
Not sure but it might as simple as improper case.
--Modules is in your structure but you keep referring to it as /modules. These should match case.
I hope it's that simple.
Don't duplicate the function names of your main bootstrap in your module bootstrap, as far as I know in ZF 1.x all of the boostraps get processed every call and I think your _initAutoload in the main boostrap is overriding the module bootstrap.
try calling your function some different like _initModuleAutoload.
At least worth a shot :)
Have you tried disabling frontController directory in application.ini config file? Try commenting/deleting this line:
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"

Full dynamic router in Zend Framework

By default you have the following URL-syntax in ZF: /module/controller/action. What i want, is to build an menu-system where i can use any URL I want.
Lets say I make an menu-item called 'news'. When i call http://www.site.com/news i want to have the folowing loaded:
module: news
controller: frontpage
action: display
These config-values must be configured in the database-record for the menu-item.
How can I do this in zend? I spend a lot of time searching for it, but I still can't figure out how to. Does anybody?
I'd suggest using a front controller plugin to scan your database for all the entries, create routing rules based on those entries and add them to the router (see this).
Of course caching strategy is recommended so that you don't do a lot of processing on every request.
You can create a plugin and in routeStartup define something that intercept your request and route /module/controller/action to /action, but for this all your action names must be unique :
class My_CustomRouterPlugin extends Zend_Controller_Plugin_Abstract
{
public function routeStartup(Zend_Controller_Request_Abstract $request)
{
$fc = Zend_Controller_Front::getInstance();
$action =$fc->getRequest()->getActionName();
$router = $fc->getRouter();
$model= new myModel();
$myPage = $model->getPageByAction($action);
$route = new Zend_Controller_Router_Route('/action', array(
'module' => $myPage->getModule();
'controller' => $myPage->getController();
'action' => $action;
));
$router->addRoute($action, $route);
return $router;
}
}
In myModel define a method can get you an object(or an array) that contains module, controller names (from you DB ).
and register this plugin in your bootstrap:
$front->registerPlugin(new My_CustomRouterPlugin());

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

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.

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.