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);
Related
I'm a newbie at Phalcon programming, I have admin/backend and frontend controllers
Admin will be served at '/admin/:controller/:action' and frontend will be served at '/:controller/:action'
Admin controllers (KalkuRitel\Controllers\Admin namespace) are located under
app/
controllers/
admin/
and frontend controllers (KalkuRitel\Controllers\Frontend namespace) are located under
app/
controllers/
frontend/
How do I accomplish this?
And how to serve 404 page within admin and frontend controllers with their own layout?
I would recommend to create modules
app/
modules/
admin/
...
frontend/
...
api/
...
register modules:
$application->registerModules(array(
'frontend' => array(
'className' => 'Application\Frontend\Module',
'path' => __DIR__ . '/../modules/frontend/Module.php'
),
'admin' => array(
'className' => 'Application\Admin\Module',
'path' => __DIR__ . '/../modules/admin/Module.php'
),
'api' => array(
'className' => 'Application\Api\Module',
'path' => __DIR__ . '/../modules/api/Module.php'
)
));
define properly Module.php files and than set route somewhat close to this:
use Phalcon\Mvc\Router as Router;
use Phalcon\CLI\Router as CliRouter;
/**
* Registering a router
*/
$di->setShared('router', function () use ($application, $config) {
if($application instanceof Phalcon\CLI\Console) {
return new CliRouter();
}
$router = new Router(false);
$router->setDefaultModule("frontend");
$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_GET_URL);
$router->removeExtraSlashes(TRUE);
foreach($application->getModules() as $key => $module) {
$prefix = $key == 'frontend' ? '' : ('/' . $key);
$router->add($prefix . '/:params', array(
'module' => $key,
'controller' => 'index',
'action' => 'index',
'params' => 1
))->setName($key);
$router->add($prefix . '/:controller/:params', array(
'module' => $key,
'controller' => 1,
'action' => 'index',
'params' => 2
));
$router->add($prefix . '/:controller/:action/:params', array(
'module' => $key,
'controller' => 1,
'action' => 2,
'params' => 3
));
}
return $router;
});
More in manual:
https://docs.phalconphp.com/pl/latest/reference/routing.html and
https://docs.phalconphp.com/pl/latest/reference/applications.html
The best way to accomplish this is to create a multiple module application.
You can learn more about multiple module application setups here:
https://docs.phalconphp.com/en/latest/reference/applications.html#multi-module
Once you have the structure for this in place you can set the routes something like this:
/*
* Dependency Injector
*/
$di = new \Phalcon\Di\FactoryDefault();
/**
* Register a router
*/
$di->setShared('router', function () {
$router = new \Phalcon\Mvc\Router();
$router->setDefaultModule('frontend');
$router->setDefaultNamespace('Multiple\Frontend\Controllers');
/*
* Frontend Routes
*/
$router->add('/:controller/:action', array(
'namespace' => 'Multiple\Frontend\Controllers',
'module' => 'frontend',
'controller' => 1,
'action' => 2
));
/*
* Backend Routes
*/
$backendRoutes = new \Phalcon\Mvc\Router\Group(array(
'namespace' => 'Multiple\Backend\Controllers',
'module' => 'backend'
));
$backendRoutes->setPrefix('/admin');
$backendRoutes->add('/:controller/:action', array(
'controller' => 1,
'action' => 3
));
$router->mount($backendRoutes);
return $router;
});
This is a quick answer and may require tweaking for your individual situation but should give you a good idea of how to accomplish your goal.
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 );
}
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.
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
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')));