Zend Routing Not Working - php

I am trying to set up my zend route using the routes.ini and bootstrap but for some reason it is not able to route as expected. My routes.ini and bootstrap.php are as follows.
routes.ini
[production]
routes.guestbook.route = "/guestbook"
routes.guestbook.defaults.controller = guestbook
routes.guestbook.defaults.action = index
bootstrap.php
protected function _initRoutes()
{
// Get Front Controller Instance
$front = Zend_Controller_Front::getInstance();
// Get Router
$router = $front->getRouter();
$router->addConfig(new Zend_Config_Ini(APPLICATION_PATH.'/configs/routes.ini', 'production'), 'routes');
}

After I've read your comment, I can assert that you can delete those statements (config and bootstrap) because what you want to achieve is the normal behavior of the zend framework default router unless you're using modules.
Thanks to FloydThreepwood who remeber me to write this detail.

The easiest way to configure routing is by using the Zend_Application_Resource_Router.
Configuration goes in your application.ini file and that's it, no further code required.
As it appears you're using a static route (no variable path components), try this in your application.ini file
resources.router.routes.guestbook.type = "Zend_Controller_Router_Route_Static"
resources.router.routes.guestbook.route = "guestbook"
resources.router.routes.guestbook.defaults.module = "default"
resources.router.routes.guestbook.defaults.controller = "guestbook"
resources.router.routes.guestbook.defaults.action = "index"
Remove the _initRoutes() method from your Bootstrap class.
Also, this is just an aside but when using other resources such as the front controller in a bootstrap _init* method, you must ensure they've been properly bootstrapped. To do so, retrieve them like this
protected function _initSomething()
{
// make sure resource is bootstrapped
$this->bootstrap('frontController');
// retrieve resource
$front = $this->getResource('frontController');
}
See http://framework.zend.com/manual/en/zend.application.theory-of-operation.html#zend.application.theory-of-operation.bootstrap.dependency-tracking

Related

How does i Get Base Directory URL in zend framework 3?

I am noobie at ZF3, We have placed zend based admin panel inside codeigniter based main app. like following way
my_app/zend_admin/
|
|
--- config
--- module
--- public
i can access zend module using www.my_app.com/zend_admin/my_zend_controller/my_zend_action.
I want to access www.my_app.com/my_ci_controller/my_ci_action.
Is there any method zend provide as ci provides base_url() so i can fetch my ci controller??
to get base URL you can use serverUrl view helper (like in codeigniter base_url())
$this->serverUrl(); // return http://web.com OR
$this->serverUrl('/uri'); // return http://web.com/uri
I am not sure about your setup but try that...
There are several ways you can get this job done using ZF micro tools.
There are some similar view helpers in the ZF like CodeIgniter has. You can use them for the purpose in the view script and layout template.
Lets start up using module.config.php of your module. You can set up base_path key under view_manager key as follows
'view_manager' => [
'base_path' => 'http://www.yoursite.com/',
]
Now if you use the following view helper
echo $this->basePath();
// Outputs http://www.yoursite.com/
If you use the following one
echo $this->basePath('css/style.css');
// Outputs http://www.yoursite.com/css/style.css
But if you do not use the above configuration
echo $this->basePath('css/style.css');
// Outputs css/style.css
As #tasmaniski said about $this->serverUrl(); you can use that too in the view script. Good thing for this does not need any configuration like $this->basePath()
What if you need this in the controller action of ZF. The easiest way to do it in the controller action is
public function indexAction()
{
$uri = $this->getRequest()->getUri();
$baseUrl = sprintf('%s://%s/', $uri->getScheme(), $uri->getHost());
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/
}
Otherwise, you can get it the following way but this works same as $this->basePath()
public function indexAction()
{
// This is for zf2
$renderer = $this->getServiceLocator->get('Zend\View\Renderer\RendererInterface');
// This is for zf3
// Assuming $this->serviceManager is an instance of ServiceManager
$renderer = $this->serviceManager->get('Zend\View\Renderer\RendererInterface');
$baseUrl = $renderer->basePath('/uri');
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/uri
}
Moreover, there are two more functions that can be used under different conditions in the controller actions. Those return empty string if rewriting rules used. Those are
$this->getRequest()->getBaseUrl();
$this->getRequest()->getBasePath();
These do not work as you expect I mean as you expect. Must refer to the issue to know why is this!

Zend Framework Custom routes setting

Zend2
Sources Files
Application
modules
default
controllers
ExampleController.php
views
scripts
form
index.phtml
library
square
form
Form_Example.php
Hi All
I am studying Zend Framework: A Beginner's guide chapter 3, i have a form_example class existing in Square/Form/Form_Example.php, it basically has a form in there.
The modules/default/controllers/ExampleControllers.php initialize it. However, i set up
resources.router.routes.example.route = /example
resources.router.routes.example.defaults.module = default
resources.router.routes.example.defaults.controller = form
resources.router.routes.example.defaults.action = form
in the application.ini
when i enter (http://localhost/zend2/public/example), it returns a Page Not Found result back, please help me with this in order to show the Form i create in Form_Example
Thanks, i am really appreciated
I use a separate file for routes, with this format and call it routes.ini
routes.example.route = /example
routes.example.defaults.module = default
routes.example.defaults.controller = form
routes.example.defaults.action = form
In your Bootstrap you then require something like this:
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
$router->addConfig($config,'routes');
I find that having a different file for routes makes my main .ini file less complex and it is all easier to understand.

Zend Framework: Controllers in separate directories

I'm quite new in Zend framework, but quickly learning. I've encountered the following problem, but I don't really know if my solution is good :)
I've created an application which uses widgets. Widget is a class which implements Widget_Interface and is executed by Widget_Manager.
Widgets can be loaded via WidgetController (which calls Widget_Manager, etc). Now the problem I encountered is: widgets can also be configured, and to make the code more transparent, I'd like a widget to have its own controller (currently, it is only a class). But the problem is, I'd like all widget configurations to be addressed via WidgetController, and then passed to specific widget controller.
An example: let's say I've got a widget named 'scrobbler'. Now when configuring it in the UI, I'd like to make Ajax request with updated settings. I could make a request like http://myapp.com/scrobbler/update-info/, so the framework would run ScrobblerController and I'd process the information from here on.
My idea is to make a request on http://myapp.com/widget/update/scrobbler/, so the framework runs WidgetController. WidgetController would then call ScrobblerController and pass other parameters.
I'm aware of _forward() function in Zend_Controller, but I'd like to have widget controllers and my application's controllers separated (let's say application controllers in /application/controllers and widget controllers in /application/controllers/widgets).
Is it possible to make this and what do I have to add to the Zend framework configuration? Hope I didn't complicate too much :)
Nice day
Edit:
Solved this using modular structure, and moved common classes into root directory.
You coud probably utilize Controller helpers instead of controllers in this case. So let's say that WidgetController is responsible for updating all types of widgets. The updateAction would need to find information on which widget type you wish to configure, this is the scrobbler parameter. You would need to name this parameter so it can be accessed easily. This can be done by either adding a route or adding the name before scrobbler in the uri.
Solution 1: Add a route:
In Bootstrap:
public function __initRoutes () {
$route = new Zend_Controller_Router_Route(
'widget/update/:type',
array (
'controller' => 'widget',
'action' => 'update'
),
array (
'type' => '[a-z_-]*'
)
);
/* #var $fc Zend_Controller_Front */
$fc = $this->bootstrap('FrontController')->getResource('FrontController');
/* #var $router Zend_Controller_Router_Rewrite */
$router = $fc->getRouter();
$router->addRoute('update-widget', $route);
}
Solution 2: Add the parameter name in the uri:
Make requests to /widget/update/type/widgetName instead.
Now, in the WidgetController::updateAction, you can fetch the widget to update using $this->_getParam('type').
So the code could look something like:
class WidgetController extends Zend_Controller_Action
{
public function updateAction ()
{
$widgetName = $this->_getParam('type');
$this->view->result = $this->_helper->Widgets->update($widgetName);
}
}
class App_Controller_Helper_Widgets extends Zend_Controller_Action_Helper
{
public function update($widgetName)
{
$widgetManager = new App_Model_WidgetManager();
$widget = $widgetManager->load($widgetName);
$widget->setOptions($this->getRequest()->getParams());
return $widget->save();
}
}

Zend Router (How to direct url such as /administration/users to Administration_UsersController)

I would like to keep the old default Zend Router, and just add a router for administration subpages since the controllers are growing in size and I would like to logically separate them a little as well as have cleaner URLs.
The documentation seems to explain how to do other things but not this...
This will work out of the box with the default routes. You just need to add an administration module, and then /administration/users will map to the users controller in the administration module.
I don't know if its possibile to do it with an Underscore and the upcase user, sorry, but without you had to add the following to your bootstrap.php
$ctrl = Zend_Controller_Front::getInstance();
$router = $ctrl->getRouter();
$route['admin_users'] = new Zend_Controller_Router_Route_Regex(
'administration/users',
array(
'controller' => 'administrationusers',
)
);
$router->addRoute('admin_users_route', $route['admin_users']);
note: in this scenario your controller is:
class AdministrationusersController extends Zend_Controller_Action
{
// stuff
}

Zend Framework - How to do custom redirect with the redirector helper?

I want to use the URL schemes in Zend Framework like /list?id=2&name=test, something like that, when I do redirection, I call this way: $this->_helper->redirector('index','list','',array('id' => 2, 'name' => 'test'));. But the URL generated will be /list/index/id/2/name/test which makes it inconsistent. How do I prepare a URL scheme like this without using custom routers (I have more than 10 controllers to do so...)?
In your bootstrap.php you can overwrite the default route
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter();
$route = new My_New_Route( ... )
$router->addRoute('default', $route);
$this->_helper->redirector uses automatically the default routers assemble method.
Unfortunately there is no integrated router that formats URLs in the way you want (with ?parameter=value& ... instead of /parameter/value)
So if you want to change this, you can make a new class
class MyRouter extends Zend_Controller_Router_Route
and overwrite the assemble function.

Categories