CakePHP: $this->set(...) is empty in view? - cakephp

I can't seem to figure out how to use routing parameters and the set-function. Here is what I did:
Config/routes.php:
Router::connect('/professions/:linkname', array('controller' => 'professions', 'action' => 'display'));
Controller/ProfessionsController.php:
public function display($linkname = null) {
$this->set('test', $linkname);
//$this->set('profession', $this->findByLinkname($linkname));
}
View/Professions/display.ctp:
<?php echo $test; ?>
when I open /professions/test, the page is empty. Why? (As you can see, $test is only a test. I commented out what I really intended to do. But that is not working either)

Ok, RFTM... found it in the official book. Passing the parameters has to be specified like so:
Router::connect('/professions/:linkname', array('controller' => 'professions', 'action' => 'display'), array('pass' => array('linkname'));
Now it works.

It explains in detail how to do what you're asking in the CakePHP Book here:
http://book.cakephp.org/2.0/en/development/routing.html#passing-parameters-to-action
Or, per Eike's answer (but in a less rude way), try this:
Router::connect(
'/professions/:linkname',
array(
'controller' => 'professions',
'action' => 'display'
),
array(
'pass' => array(
'linkname'
)
)
);
Obviously you could put this all in one line if that's easier or if it's more to your liking.

Related

How to remove controller and action name from URL in CakePHP?

How can I write working router:connect to have SEO friendly links?
In my site, I have articles and category. In right sidebar article's category is listed with below link.
<?php echo $this->Html->link(ucwords($data['Category']['name']),array('controller'=>'Articles','action'=>'displayArticles','cat'=>$data['Category']['name'])) ?>
This gives me link like - http://example.com/Articles/displayArticles/category-name , now I want the link as http://example.com/category-name. So for that I have tried below code but its not working.
Router::connect(
'/:query/*', array('controller' => 'Articles', 'action' => 'displayArticles'), array(
'params' => array('query', 'cat'),
'named' => array(
'query', 'cat'
)
)
);
So please someone let me know, how to achieve just category name(parameter) in URL.
Thanks in advance!
Router::connect(
'/:query',
array('controller' => 'Articles', 'action' => 'displayArticles',1)
array('query' => '[a-zA-Z]+')
);
Here id is numeric with regex.
please see this
You will also have to give parameter count in router.

cakephp routing with language sub directory

I am generating a link (this is for when the language is set as "fre"):
$html->link('About', array('controller' => 'pages', 'action' => 'about', 'language'=> 'fre')) ;
I also have a sub directory for languages [eng|fre] as shown above this can be either languages:
I am routing like so:
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => 'eng|fre'));
// this worked before the language subdomain
Router::connect('/about',array('controller'=>'pages','action'=>'about'));
the problem I have is, I want the urls to be:
/eng/about
/fre/about
but obviously they are coming out as:
/eng/pages/about
/fre/pages/about
I think hypothetically this should work
Router::connect('/:language/about',array(
'controller'=>'pages',
'action'=>'about',
'language' => 'eng|fre'
));
EDIT: If all 20 are pages you could try something like
Router::connect('/:language/:action',array(
'controller'=>'pages',
'action' => 'about|contact|something|else',
'language' => 'eng|fre'
));
This allows you to use any 3-character language code for any page:
Router::connect(
'/:language/:controller/:action/*',
array(),
array('language'=>'[a-z]{3}')
);

cakephp link not working

I am new in php and cakephp. Just trying to make simple navigation menu.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
its works fine if I am at www.example.com. The problem is if I am at /admin/* and i click this link, it takes me to www.example.com/admin/Entertainment, I want to go to www.wxample.com/Entertainment.
What should be my link code?
Please try:
echo $this->Html->link('Entertainment', array(
'controller' => 'UpcommingEntertainments',
'action' => 'index',
'admin' => false, // thats what you need
'plugin' => false, // could be helpful if you plan using plugins
));
I included the plugin parameter, because you could encounter the same issue, if you use plugins.
Hope that helps.
<li><?php
//pr($this->Html-);
echo $this->Html->link('Entertainment', array(
'admin' => false, // add this
'controller' => 'UpcommingEntertainments',
'action' => 'index'
));
?></li>
There is a good answer here offering a bit more indepth explanation.

CakePHP 2.x i18n route

There is some solution for using CakePHP route with params only when are not empty?
Now I code below, but I would like some better:
if(Configure::read('Config.language') !== 'en') { // en is default language
$language = '/:language';
} else {
$language = '';
}
Router::connect($language .'/'. __('register', true), array(
'controller' => 'users',
'action' => 'register'));
This code works perfectly, but I still must set language in AppHelper by url() method.
In older apps I was always duplicate Router::connect:
Router::connect('/:language/'. __('register', true), array(
'controller' => 'users',
'action' => 'register')); // for all languages without default language
Router::connect('/'. __('register', true), array(
'controller' => 'users',
'action' => 'register')); // only for default language (en)
Maybe there is simplest solutions?
You need to use 2 routes but add the 'persist' option for your language based routes. Adding 'persist' will avoid having to specify 'language' key each time when generating urls.
// for all languages without default language.
Router::connect(
'/:lang/'. __('register', true),
array(
'controller' => 'users',
'action' => 'register'
),
array(
'persist' => array('lang')
)
);
// only for default language (en)
Router::connect(
'/'. __('register', true),
array(
'controller' => 'users',
'action' => 'register'
)
);
You might also want to checkout CakeDC's I18n plugin.
Ok, these things work better, but I still other problem.
I set default language by Configure::write('Config.language'); to en in bootstrap.php
Next i wrote shema for url like this:
Router::connect('/:language/'. __('register', true), array('controller' => 'users', 'action' => 'register'), array('persist' => array('lang')));
Router::connect('/'. __('register', true), array('controller' => 'users', 'action' => 'register'));
And when users change language by beforeFilter in AppController (set new Config.language) will content from static .po and db worsk perfeclty, but links not translated.
Parametr :language works but magic function __() in Router:connect() not works.
Because first loaded is bootstrap.php, next is router.php and last is AppController.php
My question is, how to force router.php to translate links (__())?
Sorry, but still learn english...

How to route correctly when an argument is passed in a function?

I have a route:
Router::connect('/restaurants/*', array('controller'=>'restaurants', 'action' => 'view'));
that when a user accesses site.com/restaurants/Seafood, they get a list of seafood restaurants. Well, the problem is, now I want to add an edit function in my controller, and site.com/restaurants/edit/4 routes to the view function of my controller. How do I tell my routes to send /restaurants/edit to the edit() function?
I understand after the fact that the greedy star was a bad idea, but I didn't know how to make my function for view() work correctly without it. Here is my view code:
public function view($type=null) {
$this->set('title', $type.' restaurants in and near Gulf Shores');
$this->paginate['Restaurant']=array(
'limit'=>9,
'order'=>array(
'id'=>'asc'
),
'joins' => array(
array(
'table' => 'cuisines_restaurants',
'alias' => 'CuisinesRestaurant',
'type' => 'inner',
'conditions'=> array('CuisinesRestaurant.restaurant_id = Restaurant.id')
),
array(
'table' => 'cuisines',
'alias' => 'Cuisine',
'type' => 'inner',
'conditions'=> array(
'Cuisine.id = CuisinesRestaurant.cuisine_id'
)
)
)
);
$this->set('restaurantType',$this->paginate($this->Restaurant, array('cuisine_type'=>$type)));
}
This is the proper way to do routings like that:
Router::connect(
'/restaurants/:type',
array('controller'=>'restaurants', 'action' => 'view'),
array(
'pass'=>array('type'),
'type'=>'regexHere'
)
);
Router::connect(
'/restaurants/edit/:id',
array('controller'=>'restaurants', 'action' => 'view'),
array(
'pass'=>array('id'),
'id'=>'[0-9]+'
)
);
Another bright side to this way is that you can route according to the regular expression, so if someone tries to access yourwebsite/restaurants/edit/notanumber won't be routed into the edit page.
If you have a low number of controllers where you need to implement this functionality, you can do it the "quick 'n dirty" way, i.e. explicit routes:
Router::connect('/restaurants/edit/*', array('controller'=>'restaurants', 'action' => 'edit'));
(make sure to put this line above your greedy one in routes.php)
If you need this functionality for many controllers and actions, then more versatile routing would make more sense.

Resources