Prefix Folder structure for Zend - zend-framework

If i have a url such as this http://example.com/controller/action every thing works find. as expected. However i need to deploy this and in deployment things change a bit to htttp://deploy.com/stuff/pile/controller/action is there any way i can control this in zend.
Thanks in advance.

Add this route to your Bootstrap:
protected function _initRoute() {
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$router->addRoute(
'prefix_route',
new Zend_Controller_Router_Route('stuff/pile/:controller/:action',
array('controller' => $front->getRequest()->getControllerName(),
'action' => $front->getRequest()->getActionName())));
);
}

Related

How to set router for earch module in zend framework

I have 1 zend (v1) application, and 2 module : default + admin
I want when call default module will be set router in configs/router/default.ini
and if in module admin do not any thing
I tried using plugin but it doesn't work
in my plugin
class Australian_Controller_Plugin_DefaultRouter extends Zend_Controller_Plugin_Abstract {
public function routeShutdown(Zend_Controller_Request_Abstract $request) {
$currModule = $request->getModuleName();
if ($currModule != 'default') {
return;
}
$fontController = Zend_Controller_Front::getInstance();
$router1 = new Zend_Controller_Router_Rewrite();
$fontController->getRouter()->removeDefaultRoutes();
$myRoutes = new Zend_Config_Ini(APPLICATION_PATH . '/configs/router/default.ini', 'production');
$router1->addConfig($myRoutes, 'routes');
$fontController->setRouter($router1);
}
}
and /default/Bootstrap.php
protected function _initRoutes() {
$fontController = Zend_Controller_Front::getInstance();
$fontController->registerPlugin(new Australian_Controller_Plugin_DefaultRouter());
}
thanks
Note that you are adding new router after Routing so Zend already decoded address using old routes. This way you can generate URLs using new routes, but they will not e recognized by Zend. You need to call $router->route($request); again to set module/controller/action using new set of routes.
Sadly this is not working when its called in routeShutdown and has to be added to preDispatch(). Sadly I'm quite new to Zend too and still not grasping why it is so.
Code i used:
$fontController = Zend_Controller_Front::getInstance();
$router = $fontController->getRouter();
$r = new Zend_Controller_Router_Route(
'/testnew',
array(
'module' => 'user',
'controller' => 'index',
'action' => 'myaccount',
));
$router->addRoute('testnew', $r);
$router->route($request);

Zend Bootstrap How do I autoload a model?

I have an auth plugin working. I am trying to add ACL to it according to the excellent video series at http://www.youtube.com/watch?v=b6qsSnLfcmE&feature=relmfu.
My problem is that when I try to register the model in Bootstrap so that I can pass the instance to the plugin, I get a server 500 error. My bootstrap looks like this...
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initAutoload()
{
$modelLoader = new Zend_Application_Module_AutoLoader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH));
$acl = new Model_SystemAcl;
$auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($acl,$auth));
return $modelLoader;
}
}
It is the line:
$acl = new Model_SystemAcl;
That is causing the problem. If I comment it out (and the $acl parameter that is passed) it works fine. It appears as though somehow my system is not configured properly to load models. This is the entire Bootstrap shown in the tutorial btw. Perhaps there is something in Application.ini I need?
EDIT: Yes, SystemAcl.php exists and is in [applicationdir]/models
This is a full example for load models from the application namespace "Application"
$resourceLoader = new Zend_Loader_Autoloader_Resource(
array(
'basePath' => APPLICATION_PATH,
'namespace' => 'Application',
)
);
$resourceLoader->addResourceType('model', 'models/', 'Model');
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->pushAutoloader($resourceLoader);
Try to instantiate resources that may not yet have loaded i not a good practice.
You should use an Controller Plugin instead.
Based on your setup the filename of your class should be SystemAcl.php, not Model_SystemAcl.php.
if it is in application/models then i would have thought the script should be Models_SystemAcl not Model_SystemAcl (no 's'). Saying that, it is better to use plugins in the long run, rather than sticking this sort of stuff in the bootstrap. Those tutorials are good though :)

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?

Have a default controller in Zend

I have a module-based arhitecture in Zend Framework 1.11 (site.com/module/controller/action).
I've setup my site to have a default module, so that if I have the site module as default, and you go to site.com/something1/something2, it will actually take you to site.com/site/something1/something2.
I want to achieve the same thing 1 level further: say if you go to site.com/something, it should take you to site.com/site/index/something. I'm not talking about a redirect, just a re-routing.
Would something like this be possible?
If I understand correctly, it is possible and here is an example you can put in your Bootstrap:
protected function _initControllerDefaults()
{
$this->bootstrap('frontcontroller');
$front = Zend_Controller_Front::getInstance();
// set default action in controllers to "something" instead of index
$front->setDefaultAction('something');
// You can also override the default controller from "index" to something else
$front->setDefaultControllerName('default');
}
If you need the default action name to be dynamic based on the URL accessed, then I think you are looking for a custom route. In that case try:
protected function _initRoutes()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
// Custom route:
// - Matches : site.com/foo or site.com/foo/
// - Routes to: site.com/site/index/foo
$route = new Zend_Controller_Router_Route_Regex(
'^(\w+)\/?$',
array(
'module' => 'site',
'controller' => 'index',
),
array(1 => 'action')
);
$router->addRoute('actions', $route);
}

Zend framework route

I have the following controllers in my modules..
UserController.php
AdminUserController.php
Now the route for admin controller goes to: module/admin-user/ (default behaviour)
How do I make a route so all admin- will be changed to:
/admin/module/user
You will have to write a custom route.
In code:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new Zend_Controller_Router_Route(
'admin/:module/user', array('controller' => 'admin-user'));
$router->addRoute('module-admin-router', $route);
In a .ini file (I like to keep mine separate from application.ini):
[routes]
routes.module-admin-router.type = "Zend_Controller_Router_Route"
routes.module-admin-router.route = "archive/:module/user"
routes.module-admin-router.defaults.controller = "admin-user"
Then you will have to bootstrap that application section to enable the routes;
protected function _initRoutes ()
{
// setup routes here.
$front = $this->getResource('frontcontroller');
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'routes');
$router->addConfig($config->routes);
}
This route will match any admin/module/user request and send it to the AdminUserController within the matching module.
Something like that should work. Now if things get really complicated, you'll probably need to dig into the regex router - but this is as simple as I can think of it needing to be.