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.
Related
I have a ACL plugin, and I want to be able to redirect back to users/index from this plug in.. but I end up getting funny url's that don't exist. such as /cakephp/admin/acl/users/index
how can I make it go to cakephp/users/index
I have looked through the HTML helper and I'm stumped.
I will be in the cakephp chat room as well
Thanks
You can 'reset' the plugin by setting it to 'false' when creating a link;
e.g.;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false
);
update
My guess is that you're using both prefix routing and a plugin. To reset both the plugin and the prefix, do this;
echo $this->Html->link('go to user overview', array(
'controller' => 'users',
'action' => 'index',
'plugin' => false, // resets the plugin
'admin' => false, // resets the admin-prefix
);
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...
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.
I have created a route which looks like this
Router::connect('/:slug', array('controller' => 'categories', 'action' => 'view'),
array('pass' => array('slug')));
Until here, everything works okey, visiting the link http://example.com/animals-and-pets, works perfect.
On this page I have a pagination and this gives me e big problem, the links for the pages, are generating wrong, like this: http://example.com/categories/view/animals-and-pets/page:2.
The result that I want to obtain is example.com/animals-and-pets/2.
Thanks for your help in advance!
I once did it this way:
change CakePhp1.3 paginator destination url?
However, it could get much easier if you use \page:2 instead of \2
Now in cake 2.0 I call the $this->Paginator->options() to set the correct url in the view before the rest of the pagination options. Something like:
//Set the correct url for the pagination, cake will add the "page:" and "sort:" variables to this url
$this->Paginator->options(array('url'=> array('controller' => 'categories', 'action' => 'view', 'slug' => $this->params['pass'][0])));
//now display the pagination
echo $this->Paginator->counter(array('format' => __('Page {:page} of {:pages}')));
echo $this->Paginator->prev('«', array(), null, array('class' => 'prev disabled'));
echo $this->Paginator->numbers(array('separator' => ''));
echo $this->Paginator->next('»', array(), null, array('class' => 'next disabled'));
Hope this helps
I have configured slugs for all my posts and I need the Router that will do the links like:
/controller/post_slug_name
and I need this for all the controllers, but when I go with:
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
The admin panel is not working. How can I make it like this, simple, and with admin panel working? Thanks
EDIT:
With those tree routers is working as I would like, except that in the control panel I am getting even the index actions in the urls and not cool
Router::connect('/admin', array('admin' => true, 'controller' => 'settings', 'action' => 'dashboard'));
Router::connect('/admin/:controller/:action/*', array('admin' => true));
Router::connect('/:controller/:slug', array('action' => 'index'), array('pass' => array('slug')));
I have not tried this so I am not sure it works, but please try the following...
Router::connect('/:controller/:slug', array('action' => 'view:slug'));
You view function also needs to accept $slug as paramenter:
function view($slug){
...
}
If the above does not work, you could try this as well:
Router::connect('/:controller/*',array('action' => 'view'));
Once again, I have not tried any of these codes and dont know if any works, just putting ideas outthere. When I get home I will them.
Thanks,
try connecting /admin/* instead of just /admin