$this->params returns null in cakephp model - php

I decided to add some extra data about the controllers and actions in some model beforeSave as follows:
//in the model
public function beforeSave() {
$this->data[$this->alias]['path'] = 'blah blan';
debug($this->params);
die(); //for debugging!
}
The printout of debug returns null! The model I uses is the Comment model of the comments plugin. I need to access params to get the current controller, actions and some url parameters.
Indeed, I plan to change the way that comments plugin lists the comments from model based to be path based to solve the issue of need comments for more than one action depend on the same model.

Finally I found the solution: It is in Router object method getParams();
//in the model
public function beforeSave() {
$this->data[$this->alias]['path'] = 'blah blan';
debug(Router::getParams());
die(); //for debugging!
}
it prints out something like:
array(
'plugin' => null,
'controller' => 'qurans',
'action' => 'view',
'named' => array(
'comment' => '0'
),
'pass' => array(
(int) 0 => '8'
)
)

Related

Mapping slugs from database in routing

I have to support url friendly structure for a project.
There is multiple tables with a slug column, in cakephp how can I route the slug to a controller in the most efficient way.
At first I was checking if slug exist in a table, if slug exist use the route:
$c = TableRegistry::get('cateogories');
$result= $c->find()->select(['id'])->where(['url'=>$slug])->toArray();
if(count($result) > 0) {
$routes->connect(
'/:slug',
['controller' => 'Categories', 'action' => 'index', 'id' => $result[0]['id']]
);
}
The problem being that I have multiple checks like the one above and each one is being ran even if a route prior matches (doesn't need to be ran so extra querys are being called).
So how can I add a conditional statement of some sort so that it only checks if the route matches if none of the prior ones have.
I'd suggest to go for a custom route class that handles this. While you could query the data in your routes files, this is
not overly test friendly
not very DRY
not safe for reverse routing
The latter point means that when not connecting all routes, trying to generate a URL from a route array for a non-connected route might trigger an exception, or match the wrong route.
With a custom route class you could simply pass the model in the options when connecting the routes, and in the route class after parsing the URL, query that model for the given slug, and return false or the parsed data accordingly.It's really simple, just have a look at what the existing route classes do.
Here's a very basic example which should be pretty self-explantory.
src/Routing/Route/SlugRoute.php
namespace App\Routing\Route;
use Cake\Routing\Route\Route;
use Cake\ORM\Locator\LocatorAwareTrait;
class SlugRoute extends Route
{
use LocatorAwareTrait;
public function parse($url)
{
$params = parent::parse($url);
if (!$params ||
!isset($this->options['model'])
) {
return false;
}
$count = $this
->tableLocator()
->get($this->options['model'])
->find()
->where([
'slug' => $params['slug']
])
->count();
if ($count !== 1) {
return false;
}
return $params;
}
}
This example assumes that in the controller, you'd use the slug to retrieve the record. If you'd wanted to have the ID passed, then instead of using count(), you could fetch the ID and pass it along in the parsed data, like:
$params['pass'][] = $id;
It would then end up being passed as the second argument of the controller action.
routes.php
$routes->connect(
'/:slug',
['controller' => 'Articles', 'action' => 'view'],
[
'pass' => ['slug'],
'routeClass' => 'SlugRoute',
'model' => 'Articles'
]
);
$routes->connect(
'/:slug',
['controller' => 'Categories', 'action' => 'view'],
[
'pass' => ['slug'],
'routeClass' => 'SlugRoute',
'model' => 'Categories'
]
);
// ...
This would first check the Articles model, then the Categories model, etc., and stop once one of the routes finds a record for the given slug.
See also
Cookbook > Routing > Custom Route Classes
API > \Cake\Routing\Route::parse()
Source > \Cake\Routing\Route

Multiple parameters in Form->postLink at CakePHP 2.3.0

Is it possible to pass multiple parameters to an action in the controller by using the method postLink of the FormHelper?
I didn't manage to do it by using the options array. It is not very well specified in the docs what values this array admits.
This is what I tryed:
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
array('ticketId' => $ticket['Ticket']['id'], 'userId' => $staffUser['User']['id'])
));
My subscribe action looks like this:
public function subscribe($ticketId, $userId = null){
if ($this->request->is('post')) {
//...
}
}
Update
I've just noticed the provided solution creates another problem to me. Now the class stop getting added ulike before when I was using only one parameter:
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
$ticket['Ticket']['id'],
$staffUser['User']['id'],
array('class' => 'demo') //not beind added
));
postLink takes its URL entry in the same form as the create method. Parameters don't need a key, put them in order after the action key/value.
Other options need to go in the third argument - you have the options array as an additional entry to the url array.
$this->Form->postLink($staffUser['User']['_name'], array(
'action' => 'subscribe',
$ticket['Ticket']['id'],
$staffUser['User']['id']
),
array(
'class' => 'demo'
)
);
See options for FormHelper::create here:
http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html

Zend2 navigation

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();

Dispatch a Controller's Action on ZF2 route not found exception

I want want dispatch another controller action when Zf2 raise route not found exception to display my custom page ( i dont want to display a custom error page). I am working on dynamic url from database in Zf2 which will occur only when route not found.
i added a event in bootstrap function
$event->attach('route', array($this, 'loadConfiguration'), 2);
in loadConfiguration function i added to load
public function loadConfiguration(MvcEvent $e){
$application = $e->getApplication();
$sm = $application->getServiceManager();
$router = $sm->get('router');
$request = $sm->get('request');
$matchedRoute = $router->match($request);
if (null == $matchedRoute) {
$request_uri = $e->getRequest()->getrequestUri();
$dbAdaptor = $e->getApplication()->getServiceManager()->get('Zend\Db\Adapter\Adapter');
$url_table = new UrlMappingTable($dbAdaptor);
$url_data = $url_table->find($request_uri);
$controller = $url_data['controller'];
$action = $url_data['action'];
$id = $url_data['post_id'];
$original_url = $url_data['original_url'];
$alias = $sm->get('Application\Router\Alias');
$alias->setNavigation($original_url);
if(isset($url_data)){
$url = $e->getRouter ()->assemble (array('controller' => $controller,
'action' => $action ,
'id' => $id,
), array (
'name' => 'myurl'
) );
}
print 'no route match';
}
}
after getting the controller and action i just want the dispatcher to forward this controller.
I needed something similar for my project. I ended up just adding a 'catchall' rule in module.config.php
i.e.
'router' => array(
'routes' => array(
'catchAll' => array(
'type' => 'regex',
'options' => array(
'regex' => '/(?<page>.+)',
'defaults' => array(
'controller' => 'Project\Controller\MyController',
'action' => 'customPage',
),
'spec' => '/%page%',
),
),
//Other route items ...
),
//Other stuff ...
)
Place this as the first item in the routes array so it has the lowest precedence. Then you can have your customePageAction to do whatever you want!
Just something not really answering your question:
I am working on dynamic url from database in Zf2 which will occur only when route not found.
There are two ways you can achieve this much more efficiently. If there aren't much routes, you can load them before the route event. For example, you query on bootstrap all your database routes and inject them into the route stack.
Another way is creating a "catch all" route which always will match after all routes failed. Then you don't have a "route not found" but your default route is matched. This will then look for a matching database record. If none found, you just return a 404 response.
In case #1, the controller mapped by your database route is directly dispatched. In the second case, you are in your "database controller" and want to dispatch your controller mapped by the database route, you use the forward plugin:
public function matchAction()
{
// Fetch route from db here
if (!$match) {
$this->getResponse()->setStatusCode(404);
return;
}
return $this->forward($match->getController, array(
'action' => $match->getAction()
));
}

CakePHP: Can I Promote active router with Router::promote or some other way?

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'
)
);

Categories