I have a site that needs to allow multiple URL structures. For example:
www.examplesite.com/people/add // <-- example company
www.otherexample.com/xyz/people/add // <-- "xyz" company (not location based)
www.otherexample.com/florida/abc/people/add //<-- "abc" company (location based)
Each URL should be able to detect which company it is based on the URL.
So far, I've been able to parse out the URL just fine to determine which company it is, but how to I add these extra /florida/abc/ parts to the routes to allow the rest of the app to work?
I've tried a number of things including setting a variable to the '/florida/abc' (or whatever it is) at the top of the routes file, then adding that before each route, but that doesn't handle every controller/action and seems very hit or miss/buggy.
I also use the admin prefix, so for example, it would also need to work like this:
www.otherexample.com/admin/florida/abc/people/add
My assumption is that I need to use the routes.php file, but I can't determine within that how I can make this happen.
I used that approach in the web application farm.ba (not more maintained by the owner).
What I did:
Create table "nodes" with fields id, slug, model, foreign_key, type, ..
Create custom route (1),(2) class that handles Node model
After save post, store and cache slug in Node Model
After deleting the post, delete the cache and node records
This works much like wordpress routing, allows you to enter custom slug, etc.
EDIT:
Create custom route class in App/Lib/Routing/Router/MultiRoute.php like:
<?php
App::uses('CakeRoute', 'Routing/Route');
/**
* MultiRoute
*/
class MultiRoute extends CakeRoute
{
public function parse($url)
{
// debug($url); '/florida/abc/people/add'
// Add custom params
$params = array(
'location' => null,
'company' => null,
'controller' => 'peoples',
);
$params += parent::parse($url);
// debug($params);
/**
* array(
* 'location' => null,
* 'company' => null,
* 'controller' => 'peoples',
* 'named' => array(),
* 'pass' => array(
* (int) 0 => 'florida', // location
* (int) 1 => 'abc', //company
* (int) 2 => 'people', // controller
* (int) 3 => 'add' // action, default index
* ),
* 'action' => 'index',
* 'plugin' => null
* )
*
*/
// reverse passed params
$pass = array_reverse($params['pass']);
// debug($pass);
/**
* array(
* (int) 0 => 'add',
* (int) 1 => 'people',
* (int) 2 => 'abc',
* (int) 3 => 'florida'
* )
*/
if(isset($pass[3])) { $params['location'] = $pass[3]; }
if(isset($pass[2])) { $params['company'] = $pass[2]; }
// if you need load model and find by slug, etc...
return $params;
}
public function match($url)
{
// implement your code
$params = parent::match($url);
return $params;
}
}
in routes.php
App::uses('MultiRoute', 'Lib/Routing/Route');
Router::connect('/admin/*',
array('admin' => true),// we set controller name in MultiRoute class
array('routeClass' => 'MultiRoute')
);
Router::connect('/*',
array(),// we set controller name in MultiRoute class
array('routeClass' => 'MultiRoute')
);
In your controller find results using extra request params, like:
$this->request->location;
$this->request->company;
I hope this is helpful.
Creating a route for each case seems the way to go:
Router::connect('/people/add', array('controller' => 'people', 'action' => 'add'));
Router::connect('/:company/people/add', array('controller' => 'people', 'action' => 'add'), array('pass' => array('company'), 'company' => '[a-z]+'));
Router::connect('/:location/:company/people/add', array('controller' => 'people', 'action' => 'add'), array('pass' => array('company', 'location'), 'company' => '[a-z]+', 'location' => '[a-z]+'));
Then the controller can receive these values:
public function add($company = '', $location = '') {
var_dump($company, $location); exit;
}
Mind the regex in routes and amend to match your incoming data.
Related
How should I prepare my routes to deal with it, instead of addictional parts in url?
$routes = array(
/**
* Static
*/
'news' => new Zend_Controller_Router_Route('news/:page',
array('controller' => 'news', 'action' => 'index', 'page' => 1 )
),
/**
* Dynamic
*/
'thread' => new Zend_Controller_Router_Route(':slug/:page',
array('controller' => 'Thread', 'action' => 'index', 'page' => 1 )
),
e.g. example.com/thread-name-slug it shows thread with slug thread-name-slug but when I visit example.com/news it wants to show thread with slug news. I want static page here.
Thanks in advance.
The router matches routes in reverse order of their declaration. Given the request url /news, the router will attempt to match first against the route :slug/:page and, of course, finds a match, so it never gets to examine your news/:page route.
The solution is to reverse the order in which you declare the routes. Generally speaking, one wants to add generic routes before specific ones.
As the latest version of zendframework is 3.x I'll post a sample solution for Zf3, because it's not easy a complete article on zend routes.
Supouse you wanna centralize your admin requests by using only one controller; so you can check permisions, roles, etc in order to serve your site's admin pages.
We'll perform the next tasks:
Edit the "module.config.php" file to have a easy to read code.
Create a DefineRoutes.php file
Write a simple regular expression to set wildcard matching places for all posible admin tasks.
I'll supouse we creates an admin module properly registered in "modules.config.php" file
Editing the module.config.php file:
<?php
/**
* #Filename: zendframework/module/Admin/config/module.config.php
* The module required settings.
* #author: your name here
*/
return [
'controllers' => [
'factories' => include __DIR__ . '/ControllerFactories.php'
],
'router' => [
'routes' => include __DIR__ . '/DefineRoutes.php',
],
'view_manager' => ['template_path_stack' => [__DIR__ . '/../view',],],
];
Note: we do not use the close tag ?> in our files
Creating the "DefineRoutes.php" file.
<?php
/**
* #Filename: zendframework/module/Admin/config/DefineRoutes.php
* Declares site's admin routes
* #author: your name here
*/
namespace Admin;
use Zend\Router\Http\Segment;
// first a couple of useful functions to make our life easy:
// Creates a regexp to match all case-variants of a word
function freeCaseExt($toCase){
$len = strlen($toCase);
$out = '';
if($len < 1){ return $out; }
for ($i=0; $i<$len; $i++){
$s = strtolower(substr($toCase, $i, 1));
$out .= '['.$s.strtoupper($s).']';
}
return $out;
}
// To append slash child routes elsewhere
function SlashUri($controller, $action){
return [
'type' => \Zend\Router\Http\Literal::class,
'options' => [
'route' => '/',
'defaults' => ['controller' => $controller, 'action' => $action ]]];
}
$adminvariants = freeCaseExt('admin'); // to constrain our main route
// Our route family tree:
'admin' => [
'type' => Segment::class,
'options' => [
'route' => '/:admin[/:case][/:casea][/:caseb][/:casec][/:cased][/:casee][/:casef][/:caseg][/:caseh]',
'constraints' => [
'admin' => $adminvariants,
'case' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'casea' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'caseb' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'casec' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'cased' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'casee' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'casef' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'caseg' => '[a-zA-Z0-9][a-zA-Z0-9_-]*',
'caseh' => '[a-zA-Z0-9][a-zA-Z0-9_-]*'
],
'defaults' => [
'controller' => Controller\AdminController::class,
'action' => 'index'
]
],
'may_terminate' => TRUE,
'child_routes' => [
'adminslash' => SlashUri(Controller\AdminController::class, 'index'),
]
],
// Now you have declared all the posible admin routes with or without
// slaches '/' at 9 deep levels using the AdminController::Index() method
// to decide wath to do.
IMPORTANT: As we defined a first level wildcard :admin a proper constraint is required or it overlaps other first level routes.
The controllers logics is a few out of skope.
Hope this idea helps somebody.
Luis
I have the following situation: Contacts without a first or last name, in fact, they only have a email address.
I can work with these contacts fine, but when I use the listview anywhere (for instance to show all contacts from a company) there now is no way to click through to the contact (normally you would click on the name).
I'm looking for a way to solve this, for instance by showing a clickable text like 'name not known', but can't figure out how to do this. I've been looking at the manual and in the files in the modules directory and the sugarfields dir, but can't quite figure it out.
The closest I got was in /sugarcrm/modules/Contacts/metadata/listviewdefs.php
where this piece of code resides:
$listViewDefs['Contacts'] = array(
'NAME' => array(
'width' => '20%',
'label' => 'LBL_LIST_NAME',
'link' => true,
'contextMenu' => array('objectType' => 'sugarPerson',
'metaData' => array('contact_id' => '{$ID}',
'module' => 'Contacts',
'return_action' => 'ListView',
'contact_name' => '{$FULL_NAME}',
'parent_id' => '{$ACCOUNT_ID}',
'parent_name' => '{$ACCOUNT_NAME}',
'return_module' => 'Contacts',
'return_action' => 'ListView',
'parent_type' => 'Account',
'notes_parent_type' => 'Account')
),
'orderBy' => 'name',
'default' => true,
'related_fields' => array('first_name', 'last_name', 'salutation', 'account_name', 'account_id'),
),
Somewhere there has to be a function that joins the first and lastname together...
Edit: I found a solution:
The actual concatenation function is in /sugarcrm/include/SugarObjects/templates/person/person.php and is called _create_proper_name_field()
I can modify the output for my specific case by adding something like this to the end of the function:
if (empty(trim($full_name))){
$full_name = 'Name unknown';
}
However, I would rather have a upgrade safe solution, so that will be the next challenge.
Don't edit the core because the next upgrade will break your SugarCRM instance. Use logic hooks to be upgrade safe:
create a file 'logic_hooks.php' in /custom/modules/Contacts/
In that file, add the followin code:
<?php
$hook_array['before_save'][] = Array(1,'logic_fill_name','custom/modules/Contacts/logic_hooks/logics.php','ContactLogics','logic_fill_name');
After you have done this. create the file 'logics.php' in /custom/modules/Contacts/logic_hooks.
In the logics.php file, add something like:
<?php
require_once 'include/SugarQuery/SugarQuery.php';
/**
* Class ContactLogics
*/
class ContactLogics {
/**
* #param $bean
* #param $event
* #param $arguments
*/
public function logic_fill_name($bean, $event, $arguments) {
if (empty(trim($bean->first_name)) && empty(trim($bean->last_name))){
$bean->last_name = 'Name unknown';
}
}
}
Now some explanation. When you edited a recordview and pressed the save button, the logic hook 'before_save' will be triggered. This code will change the full name to 'Name unknown' when the full name is empty. When the 'before_save' is executed, the actual save will take place.
I'm writing my custom router for a Zend Framework 2 project extending Zend\Mvc\Router\RouteInterface. The routes should come from a database (large project with hundreds of pages). A working Router obviously only needs two methods: match() and assemble(). The match one I got working alright.
But what about assemble()? What should this method return? Could it be it only returns the base path of the Application?
Here is what one of the internal routers (Zend\Mvc\Router\SimpleRouteStack) of ZF2 does:
/**
* assemble(): defined by RouteInterface interface.
*
* #see \Zend\Mvc\Router\RouteInterface::assemble()
* #param array $params
* #param array $options
* #return mixed
* #throws Exception\InvalidArgumentException
* #throws Exception\RuntimeException
*/
public function assemble(array $params = array(), array $options = array())
{
if (!isset($options['name'])) {
throw new Exception\InvalidArgumentException('Missing "name" option');
}
$route = $this->routes->get($options['name']);
if (!$route) {
throw new Exception\RuntimeException(sprintf('Route with name "%s" not found', $options['name']));
}
unset($options['name']);
return $route->assemble(array_merge($this->defaultParams, $params), $options);
}
Reference: Custom Routing in Zend Framework 2
Basically assemble is what would be call when you do things like $this->redirect-toRoute($name, $params);
so it should return a URL string based on the route config. that the route can match using the same route config.
When you call toRoute the routestack that you posted finds the route with the name you specified in the call and then asks it to assemble the URL to that route
'test' => array(
'type' => 'Segment',
'options' => array(
'route' => '/test[/:id]',
'constraints' => array(
'id' => '[a-zA-Z][a-zA-Z0-9_-]*',
),
'defaults' => array(
'__NAMESPACE__' => 'Application\Controller',
),
),
),
this route named 'test' when we call $this->redirect-toRoute('test', array('id' => 1)); the route stack will find the instantiated route for 'test', this is a \Zend\Mvc\Router\Http\Segment and then calls it assemble function which will take the params send with in the call to toRoute and it will yield a URL string like this
/test/1
and that's basically what the assemble function does.
I would like to make navigation buttons in my view, for example index.phtml but it's not working. I did know how to do it in Zend1 but in Zend2 I have a problem. My code looks like this (file index.phtml):
$container = new \Zend\Navigation\Navigation($tableActions);
var_dump($container);
echo '<div class="table-column">';
echo $this->navigation($container)->menu();
echo '</div>';
Variable $tableAction looks like this:
public $tableActions = array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
);
I did not get any error, just whole site die on this line. var_dump returns object(Zend\Navigation\Navigation) so it's fine so far. Problem is, how to show it...
The navigation pages have dependencies which aren't being met by just creating a new container class in a view. The Mvc page needs a RouteStackInterface (Router) instance and a RouteMatch instance. Similarly Uri pages need the current Request instance.
You can see this clearly if you take a look at the Zend\Navigation\Service\AbstractNavigationFactory and its preparePages and injectComponents methods.
The view is not the right place to be instantiating menus, instead put the menu configuration spec in your module.config.php...
<?php
return array(
'navigation' => array(
'table_actions' => array(
array(
'label' => 'On/Off',
'module' => 'import',
'controller' => 'import',
'action' => 'setstatus',
'params' => array('id' => null),
),
),
),
);
Write a factory extending the AbstractNavigationFactory class and implement the getName() method which returns the name of your menu spec key (table_actions in this example)
<?php
namespace Application\Navigation\Service;
use Zend\Navigation\Service\AbstractNavigationFactory;
class TableActionsFactory extends AbstractNavigationFactory
{
/**
* #return string
*/
protected function getName()
{
return 'table_actions';
}
}
Map the factory to a service name in the service_manager spec of module.config.php ...
<?php
return array(
'navigation' => array(// as above ... )
'service_manager' => array(
'factories' => array(
'TableActionsMenu' => 'Application\Navigation\Service\TableActionsFactory',
),
),
);
Now you can call the view helper using the service name TableActionsMenu you just mapped
<div class="table-column">
<?php echo $this->navigation('TableActionsMenu')->menu(); ?>
</div>
Finally, if, as I suspect, you need to change an attribute of the page depending on the view, you can do that too, navigation containers have find* methods which can be accessed from the navigation helper and used to retrieve pages.
Here's an example looking for the page with a matching page label, then changing it before rendering (obviously not an ideal search param, but it gives you the idea)
$page = $this->navigation('TableActionsMenu')->findOneByLabel('On/Off');
$page->setLabel('Off/On');
// and then render ...
echo $this->navigation('TableActionsMenu')->menu();
I've got two or more routes that will be going to the same controller and action. This is fine until I want to use a helper such as the form helper or pagination on the page.
What happens is that the current url changes to whatever is declared first in my routes.php file.
I see there is a way to promote a router with Router::promote but I'm not sure if I can do it based on the current url or router being used or if there's a bett way to do this.
Here's an example of what my router.php looks like:
Router::connect('/cars-for-sale/results/*', array('controller' => 'listings', 'action' => 'results'));
Router::connect('/new-cars/results/*', array('controller' => 'listings', 'action' => 'results'));
Router::connect('/used-cars/results/*', array('controller' => 'listings', 'action' => 'results'));
Let's say for example that I'm at the url domain.com/used-cars/results/ and I'm using the form helper or pagination helper, the url that is being put in the action or href is domain.com/cars-for-sale/results/.
Any help or info would be appreciated.
Routes should be unique and identifiable!
The problem with these Routes is that, basically, you created duplicate URLs not only does this cause problems with CakePHP picking the right route, Google doesn't like that as well; duplicated content will have a negative effect on your SEO ranking!
In order to pick the right URL (Route), CakePHP should be able to do so, based on its parameters; your current Routes do not offer any way to distinguish them.
And neither does your application!
All these URLs will present the same data;
/cars-for-sale/results/
/new-cars/results/
/used-cars/results/
Solution 1 - separate actions
If your application is limited to these three categories, the easiest solution is to create three actions, one per category;
Controller:
class ListingsController extends AppController
{
const CATEGORY_NEW = 1;
const CATEGORY_USED = 2;
const CATEGORY_FOR_SALE = 3;
public $uses = array('Car');
public function forSaleCars()
{
$this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_FOR_SALE)));
}
public function newCars()
{
$this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_NEW)));
}
public function usedCars()
{
$this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => self::CATEGORY_USED)));
}
}
Routes.php
Router::connect(
'/cars-for-sale/results/*',
array('controller' => 'listings', 'action' => 'forSaleCars')
);
Router::connect(
'/new-cars/results/*',
array('controller' => 'listings', 'action' => 'newCars')
);
Router::connect(
'/used-cars/results/*',
array('controller' => 'listings', 'action' => 'usedCars')
);
Solution 2 - Pass the 'category' as parameter
If the list of URLs to be used for the 'listings' will not be fixed and will expand, it may be better to pass the 'filter' as a parameter and include that in your routes;
routes.php
Router::connect(
'/:category/results/*',
array(
'controller' => 'listings',
'action' => 'results',
),
array(
// category: lowercase alphanumeric and dashes, but NO leading/trailing dash
'category' => '[a-z0-9]{1}([a-z0-9\-]{2,}[a-z0-9]{1})?',
// Mark category as 'persistent' so that the Html/PaginatorHelper
// will automatically use the current category to generate links
'persist' => array('category'),
// pass the category as parameter for the 'results' action
'pass' => array('category'),
)
);
Read about the Router API
In your controller:
class ListingsController extends AppController
{
public $uses = array('Car');
/**
* Shows results for the specified category
*
* #param string $category
*
* #throws NotFoundException
*/
public function results($category = null)
{
$categoryId = $this->Car->Category->field('id', array('name' => $category));
if (!$categoryId) {
throw new NotFoundException(__('Unknown category'));
}
$this->set('cars', $this->Paginator->paginate('Car', array('Car.category_id' => $categoryId)));
}
}
And, to create a link to a certain category;
$this->Html->link('New Cars',
array(
'controller' => 'listings',
'action' => 'results',
'category' => 'new-cars'
)
);