Zend Framework index in the url - php

I'm writing a Zend Framework application and its not a big deal but I can't figure out (even after googling) how to remove the /index/ from the url
So I currently have this
http://myapplication.local/index/home
When I really want
http://myapplication.local/home
I understand it may be possible to do this via the .htaccess?

The issue occurs because Zend by default uses controller/action urls (which is a default route). Because your root controller is IndexController and the action is IndexController::homeAction it is accessed via index/home.
The easiest way to do what you want is adding routes to the application.ini as follows:
resources.router.routes.home_route_or_any_name.route = "home"
resources.router.routes.home_route_or_any_name.defaults.module = "default"
resources.router.routes.home_route_or_any_name.defaults.controller = "index"
resources.router.routes.home_route_or_any_name.defaults.action = "home"
You can change home_route_or_any_name to anything you want. You can also add many routes definitions to fit your needs.
For more information refer to Zend Framework Documentation

You can try this in bootstrap.php
/**
* Setup Routig.
* Now all calls are send to indexController like
* URL/ACTION-1
* URL/ACTION-2
*
* #return void
**/
protected function _initRouters()
{
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(
':action/*',
array(
'controller' => 'index',
'action' => 'index'
)
);
$router->addRoute('default', $route);
}
I will remove index from all the action generated from indexController.
OR
in application.ini
routes.index.type = "Zend_Controller_Router_Route"
routes.index.route = "/"
routes.index.defaults.module = "default"
routes.index.defaults.controller = "index"
routes.index.defaults.action = "index"
For molre detail on routing you can read here

Related

Phalcon php multilingual routing

Hi I'm new to the php world.
I'm wondering What is the best way to handle multilingual routing ?
I'm starting to create a website with Phalcon php.
I have the following routing structure.
$router->add('/{language:[a-z]{2}}/:controller/:action/:params', array(
'controller' => 2,
'action' => 3,
'params' => 4,
));
$router->add('/{language:[a-z]{2}}/:controller/:action', array(
'controller' => 2,
'action' => 3,
));
$router->add('/{language:[a-z]{2}}/:controller', array(
'controller' => 2,
'action' => 'index',
));
$router->add('/{language:[a-z]{2}}', array(
'controller' => 'index',
'action' => 'index',
));
My problem is for instance when I go on mywebsite.com/ I want to change my url in the dispatcher like mywebsite.com/en/ or other language. Is it a good practise to handle it in the beforeDispatchLoop ? I seek the best solutions.
/**Triggered before entering in the dispatch loop.
* At this point the dispatcher don't know if the controller or the actions to be executed exist.
* The Dispatcher only knows the information passed by the Router.
* #param Event $event
* #param Dispatcher $dispatcher
*/
public function beforeDispatchLoop(Event $event, Dispatcher $dispatcher)
{
//$params = $dispatcher->getParams();
$params = array('language' => 'en');
$dispatcher->setParams($params);
return $dispatcher;
}
This code doesn't work at all, my url is not change. The url stay mywebsite.com/ and not mywebsite.com/en/
Thanks in advance.
I try one solution above.
The redirect doesn't seems to work. I even try to hard-coded it for test.
use Phalcon\Http\Response;
//Obtain the standard eventsManager from the DI
$eventsManager = $di->getShared('eventsManager');
$dispatcher = new Phalcon\Mvc\Dispatcher();
$eventsManager->attach("dispatch:beforeDispatchLoop",function($event, $dispatcher)
{
$dispatcher->getDI->get('response')->redirect('/name/en/index/index/');
}
I think you are confusing something. If I understand correctly: you want to redirect users to a valid url if they open a page without specifying a language.
If that's the case you should verify in your event handler whether the language parameter is specified and redirect user to the same url + the default language if its missing. I am also assuming that your beforeDispatchLoop is located in your controller, which is also part of the problem, because your route without a language never matches and you never get into that controller. Instead you need to use it as an event handler with the event manager as per the documentation. Here is how you do the whole thing.
$di->get('eventsManager')->attach("dispatch:beforeDispatchLoop", function(Event $event, Dispatcher $dispatcher)
{
$params = $dispatcher->getParams();
// Be careful here, if it matches a valid route without a language it may go into a massive
// recursion. Really, you probably want to use `beforeExecuteRoute` event or explicitly
// check if `$_SERVER['REQUEST_URI']` contains the language part before redirecting here.
if (!isset($params['language']) {
$dispatcher->getDI->get('response')->redirect('/en' . $_SERVER['REQUEST_URI']);
return false;
}
return $dispatcher;
});

How check if action exsist in Controller Plugin preDispatch

I have two modules (default and mobile) the module mobile is a rewrite the default portal in jquery mobile but with much less controllers and actions!
I thought of write a controller plugin that check if controller and action exist in module mobile, if not I would like overwrite the module mobile to default.
I try this:
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
if ($request->getModuleName() == 'mobile') {
if (!$dispatcher->isDispatchable($request)) {
// Controller or action not exists
$request->setModuleName('default');
}
}
return $request;
}
but $dispatcher->isDispatchable($request) return always true though the action not exist! :S
and i receive "Action foo does not exist and was not trapped in __call()"
How can I do?
Thanks
Have you ever wondered how to check if a controller/action exist in zend FM from any side of app ? Here is the code
$front = Zend_Controller_Front::getInstance();
$dispatcher = $front->getDispatcher();
$test = new Zend_Controller_Request_Http();
$test->setParams(array(
'action' => 'index',
'controller' => 'content',
)
);
if($dispatcher->isDispatchable($test)) {
echo "yes-its a controller";
//$this->_forward('about-us', 'content'); // Do whatever you want
} else {
echo "NO- its not a Controller";
}
EDIT
Check like this way
$classMethods = get_class_methods($className);
if(!in_array("__call", $classMethods) &&
!in_array($this->getActionMethod($request), $classMethods))
return false;
and also please see detail link
I would suggest you make a static or dynamic routes either via config resource manager, bootstrap or via front controller plugin:
Example of defining static routes in Bootstrap.php:
public function _initRoutes()
{
$front = Zend_Controller_Front::getInstance();
$router = $front->getRouter(); // default Zend MVC routing will be preserved
// create first route that will point from nonexistent action in mobile module to existing action in default module
$route = new Zend_Controller_Router_Route_Static(
'mobile/some-controller/some-action', // specify url to controller and action that dont exist in "mobile" module
array(
'module' => 'default', // redirect to "default" module
'controller' => 'some-controller',
'action' => 'some-action', // this action exists in "some-controller" in "default" module
)
);
$router->addRoute('mobile-redirect-1', $route); // first param is the name of route, not url, this allows you to override existing routes like default route
// repeat process for another route
}
This would effectively route request for /mobile/some-controller/some-action to /default/some-controller/some-action
some-controller and some-action should be replaced with proper controller and action names.
I was using static routing which is ok if you route to exact urls, but since most applications use additional params in url for controller actions to use, it is better to use dynamic routes.
In above example simply change route creation class to Zend_Controller_Router_Route and route url to "mobile/some-controller/some-action/*" and every request will be routed dynamically like in example:
/mobile/some-contoller/some-action/param1/55/param2/66
will point to
/default/some-controller/some-action/param1/55/param2/66
For more info about routing in ZF1 check this link

Zend Framework URL Rewrite For SEO

I'm new to Zend, so pls help me out on this one, since I have searched a lot and tried a lot, I really did.
here is the very old question:
this is my current url:
www.sample.com/blog/detail/index/id/5
wanted:
www.sample.com/url-rewrite-tips
well, I put following code in the bootstrap.php
$router = Zend_Controller_Front::getInstance()->getRouter();
$route = new Zend_Controller_Router_Route(
'url-rewrite-tips',
array(
'module' => 'blog',
'controller' => 'detail',
'action' => 'index',
'id' => '5'
)
);
$router->addRoute('url-rewrite-tips', $route);
it was working, but it also is a hardcode. i tried to get param in bootstrap.php, but failed.
Now, I have more than 100 ids. However, I tried to put it in index.pthml, in a foreach(){} , then it fails.
I have rewrite names in the DB for every article, how am I supposed to it?
better not use configs or .htaccess
Thanks In advanced.
What I always do is I add the routes.ini execution in bootstrap:
protected function _initRoutes()
{
$this->bootstrap('FrontController');
$front = $this->getResource('FrontController');
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', 'production');
$router = $front->getRouter();
$router->addConfig($config, 'routes');
}
Then in routes.ini:
[production]
routes.content.route = "/s/:permalink"
routes.content.defaults.controller = "content"
routes.content.defaults.action = "show"
routes.content.reqs.permalink = "[a-z0-9-]+"
So now when someone access http://mydomain.com/s/some-nice-permalink routing goes to ContentController ShowAction() method, which takes permalink as param and finds it in database.
Of course you'll have to also write methods for creating unique permalinks in database.

Zend_Controller_Router_Route Chaining Problem with more than 3 url parameters

I can't seem to figure out what's going wrong, but I'm attempting to setup module routing based on sub domain. Otherwise the routing is standard. The following works until I add more than 3 parameters in the URL:
This is within a controller plugin
...
public function routeStartup() {
$router = Zend_Controller_Front::getInstance()->getRouter();
$pathRoute = new Zend_Controller_Router_Route (
':controller/:action/*',
array(
'controller' => 'index',
'action' => 'index'
)
);
$hostRoute = new Zend_Controller_Router_Route_Hostname(':module.domain.com');
$chainedRoute = $hostRoute->chain($pathRoute);
$router->addRoute('host', $chainedRoute);
...
}
http://module.domain.com/controllerName/actionName/param1 works
http://module.domain.com/controllerName/actionName/param1/param2 does not work
Has anyone else run into this?
Looks like a bug in the framework routing code.
See http://framework.zend.com/issues/browse/ZF-6654 for a fix.

Zend_Router, routing to indexController / forward when parameter provided

is there any way to tell the Zend Framework Router:
" if www.domain.com/test " is called
forward to indexController -
forwardAction and recieve the parameter.
" if www.domain.com" is
called show indexController /
indexAction
??
Set this function up into your bootstrap file or wherever you store your rutes:
protected function _initRouter()
{
$this->bootstrap('frontController');
$front = $this->getResource('frontController');
$router = $front->getRouter();
$router->addRoute('testToForward',
new Zend_Controller_Router_Route(
':parameter',
array(
'controller' => 'index',
'action' => 'forward',
}
Edit: This way the passed parameter will be passed as 'parameter' to the forward controller.
The second route is provided by default.

Categories