ZF1 Routing Not working for View Script Sub-directories - php

I am using ZF1 and am trying to figure out why the view/scripts sub directories routing is not working. Here is the code from the bootstrap adding the routes. Please let me know what I may have wrong. Thanks for your time.
public function _initRoutes()
{
$controller = Zend_Controller_Front::getInstance();
$router = $controller->getRouter();
//Route for user Account
$account = new Zend_Controller_Router_Route(
'ecommerce/account/:action',
array(
'module' => 'ecommerce',
'controller' => 'user_account',
'action' => 'index'
)
);
//Route for user Cart
$cart = new Zend_Controller_Router_Route(
'ecommerce/cart/:action',
array(
'module' => 'ecommerce',
'controller' => 'user_cart',
'action' => 'index'
)
);
//die(print_r($account));
$router->addRoute('ecommerce/user_account/', $account);
$router->addRoute('ecommerce/user_cart/', $cart);
}

you have to try this code
$router = Zend_Controller_Front::getInstance();
$router1 = $router->getRouter();
$router1->addRoute('category/:id/:name/*',
new Zend_Controller_Router_Route('category/:user_id/:user_name/*', array(
'controller' => 'user',
'action' => 'index'
))
);
This will help you.

Related

Phalcon route doesn't work

My phalcon app has worked fine with standard MVC route convention.
However, I want to handle some variable via URL, then I have a route:
$router = new \Phalcon\Mvc\Router();
$router->add("/timesheet/some/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::some");
$router->add("/timesheet/getreport/{type:[a-z]}/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::getreport");
$router->addPost("/user/auth", "User::auth");
return $router;
The first route (timesheet/some) worked fine, I can access to "year", "month" variable using $year = $this->dispatcher->getParam("year");, however the second route (timesheet/getreport) doesn't work. In this case, $year = $this->dispatcher->getParam("year"); return null.
If I changed to
$router = new \Phalcon\Mvc\Router(false);
$router->add("/:controller/:action", array(
"controller" => 1,
"action" => 2,
));
$router->add("/timesheet/some/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::some");
$router->addPost("/timesheet/getreport/{type:[a-z]}/{year:[0-9]+}/{month:[0-9]{2}}/{day:[0-9]{2}}", "Timesheet::getreport");
$router->addPost("/user/auth", "User::auth");
return $router;
every request will be routed to index/index. My project URL is localhost/fpas, and I already try both route /fpas/timesheet/some and /timesheet/some but it always redirect to index/index. What's wrong with it? (security/auth is commented out, so it's not result from authentication).
From my understand, the default route, $router = new \Phalcon\Mvc\Router(); only allow you to follow MVC convention, while $router = new \Phalcon\Mvc\Router(false); do but you have to specific all routes for every controller/action. Can I keep the convention for most of the action, while have specific rewrite routes for some action. How can I do that?
Thank you very much.
It works for me:
$router = new Phalcon\Mvc\Router();
$router->add("/", array(
'controller' => 'index',
'action' => 'setLanguage',
));
$router->add("/{language:[a-z]{2}}", array(
'controller' => 'index',
'action' => 'index',
'language' => 1
));
this one get's default routing just with language in the beginning
$router->add("/{language:[a-z]{2}}/:controller/:action", array(
'controller' => 2,
'action' => 3,
'language' => 1
));
with default action "index" when it's not in url
$router->add("/{language:[a-z]{2}}/:controller", array(
'controller' => 2,
'action' => 'index',
'language' => 1
));
some other routes
$router->add("/{language:[a-z]{2}}/:controller/:action/:params", array(
'controller' => 2,
'action' => 3,
'language' => 1,
'params' => 4
));
$router->add("/{language:[a-z]{2}}/question/add/{type}", array(
'language' => 1,
'controller' => 'question',
'action' => 'add',
));

Zend Translate Routing for custom modules

How do I have to set up the routing for a default language in Zend Framework on a custom module, in my case the admin module.
I have the following code:
$langRoute = new Zend_Controller_Router_Route(
':lang/admin',
array(
'lang' => 'ro',
)
);
what i wan't to obtain is url's like the following:
www.example.com/ro/admin/pages/add/62
www.example.com/ro/admin/pages/index/by/date_modified/order/asc
etc.
Try this will working:
protected function _initRoutes() {
$langRoute = Zend_Controller_Front::getInstance ()->getRouter ();
$langRoute->removeDefaultRoutes ();
$route = new Zend_Controller_Router_Route(
':lang/:module/:controller/:action/*',
array (
'lang' => 'ro',
'module' => 'admin',
'controller' => 'index',
'action' => 'index'
)
);
$langRoute->addRoute ( 'langrouter', $route );
}

How router/roites could be injected in application in phalcon framework?

Here, in docs, is written about how to create routes:
http://docs.phalconphp.com/en/latest/reference/routing.html
But i can't find how i can inject them in application.
What i need to do, to make my application use defined routes?
Should i inject router (or how?)
The router can be registered in the DI (in your public/index.php) this way:
$di->set('router', function() {
$router = new \Phalcon\Mvc\Router();
$router->add("/login", array(
'controller' => 'login',
'action' => 'index',
));
$router->add("/products/:action", array(
'controller' => 'products',
'action' => 1,
));
return $router;
});
Also is possible to move the registration of routes to a separate file in your application (i.e app/config/routes.php) this way:
$di->set('router', function(){
require __DIR__.'/../app/config/routes.php';
return $router;
});
Then in the app/config/routes.php file:
<?php
$router = new \Phalcon\Mvc\Router();
$router->add("/login", array(
'controller' => 'login',
'action' => 'index',
));
$router->add("/products/:action", array(
'controller' => 'products',
'action' => 1,
));
return $router;
Examples: https://github.com/phalcon/php-site/blob/master/public/index.php#L33 and https://github.com/phalcon/php-site/blob/master/app/config/routes.php

Zend Custom Modules Routes

i have this set up:
application
---admin
-----controllers
-------IndexController.php
---public
-----controllers
---modules
-----users
-------controllers
-----pages
-------controllers
I'd like to have this routes :
www.domain.com/admin/modulename/controller/action/
or if module not exist i use admin controllers
www.domain.com/admin/controller/action/
Any suggesiton?
Thanks
In your bootstrap file write this code :
$router = Zend_Controller_Front::getInstance()->getRouter();
if($moduleName){
$route = new Zend_Controller_Router_Route(
':module/:controller/:action/',
array(
'controller' => $ControllerName,
'module' => '$moduleName',
'action' => $ActionName
)
);
}else{
$route = new Zend_Controller_Router_Route(
':controller/:action/',
array(
'controller' => $ControllerName,
'action' => $ActionName
)
);
}
$router->addRoute('default', $route);

SocialEngine module with custom routes

I'm developing a module for the SocialEngine package and I'd like to be able to specify multiple custom routes.
Currently I'm editing the Bootstrap.php file found in the directory of my module with the following;
class Courses_Bootstrap extends Engine_Application_Bootstrap_Abstract
{
protected function _initRouter(){
$fc = Zend_Controller_Front::getInstance();
$router = $fc->getRouter();
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/activity/:activity_id', array('module' => 'courses', 'controller' => 'index','action' => 'activity')));
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/course/edit/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'edit')));
$router->addRoute('courses', new Zend_Controller_Router_Route('courses/course/create/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'create')));
return $router;
}
}
However it would seem that when I specify more then 1 route all routes stop passing in the custom variable (course_id or activity_id)
I'm retrieving the variable as follows;
$course_id = $this->getRequest()->getParam("course_id");
I've taken the approach from here;
http://tjgamble.com/2011/04/adding-custom-routes-to-your-socialengine-4-modules/
Many thanks,
Andy
you have to give them different names:
$router->addRoute('courses_activitiy', new Zend_Controller_Router_Route('courses/activity/:activity_id', array('module' => 'courses', 'controller' => 'index','action' => 'activity')));
$router->addRoute('courses_course', new Zend_Controller_Router_Route('courses/course/edit/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'edit')));
$router->addRoute('courses_create', new Zend_Controller_Router_Route('courses/course/create/:course_id', array('module' => 'courses', 'controller' => 'course','action' => 'create')));

Categories