CAKEPHP - how to leave the plug in url - cakephp

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

Related

How to redirect in CakePHP with # in url, and not %23?

I am using CakePHP and I am doing this:
$this->redirect(array('controller' => 'users', 'action' => 'view',$id));
output in the browser: .../users/view/42
Since I am using JQueryUI tabs, I want the user to be redirected to the tab he just edited, so it should looks something like :
$this->redirect(array('controller' => 'users', 'action' => 'view',$id."#groups"));
output in the browser: .../users/view/42%23groups
But expected result: .../users/view/42#groups
Q: How to send a correct url with #id in it to send the focus ?
I want, if possible, not use a custom GET param that echo a js in the view.ctp to get the focus asked.
I hope it is a CakePHP issue and don't need to change some .htaccess like (How to rewrite a URL with %23 in it?)
Please read the documentation
http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::url
$this->redirect(array(
'controller' => 'users', 'action' => 'view', $id, '#' => 'groups'));
This is how I achieved it:
// Redirect to the home page.
$this->redirect(array(
'controller' => 'cars',
'action' => 'index',
'#' => 'thankyou'
));

Weird redirect issue when using Auth and admin prefix in CakePHP

I'm using the admin prefix in my Cakephp app, for some admin views. I'm also using Auth to restrict access to those views, based on a role field in the User table. Pretty standard.
The problem is, that when an unauthorized user tries to go to, say, admin/users, (in this case the index action is prohibited), they are redirected to /admin/users/login which of course, doesn't exist.
This doesn't happen with actions that do not have the admin prefix. Those behave just fine.
Why are users being sent to to a login that is prepended by the admin prefix and the prohibited action?
Anyone who is still having trouble with this, according to the documentation you can use an array or a string in loginAction (Documentation).
Using an array and setting 'admin' => false was still giving me trouble, so I tried using a string instead:
public $components = array(
'Auth' => array(
'loginRedirect' => array('controller' => 'dashboards', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
'loginAction' => '/users/login',
'authorize' => array('Actions')
),
);
This ended up solving my problem. Hopefully it works for you as well.
You need to override the specific prefix in the routing array.
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login',
'admin' => false
);
or, if you're using multiple prefixes, you can dynamically remove the prefix name like this:
$this->Auth->loginAction = array(
'controller' => 'users',
'action' => 'login',
$this->request->prefix => false
);

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 display name in the url instead id in Cakephp?

I have created an application in cakephp environment,
I am displaying the user profile using user_id in user profile page
I am sending the value to controller using anchor tag like below:
(I can't send the name because it is not unique).
View Profile
Function in User Controller :
function viewprofile($userid){
// my logic
}
the profile url is like this:
http://www.xyz.com/users/12
I want to display the name of user instead user_id
How can we display name(with slug) in url in cakephp
Please help me
echo $this->Html->link('View Profile', array(
'controller' => 'users',
'action'=> 'view',
$result['user']['slug']
));
you have to do 3 things.
Create unique User Slugs (you might have that already ;-) )
Create your links with the code Dave mentioned before
echo $this->Html->link('View Profile', array('controller' => 'users','action'=> 'view',$result['user']['slug']));
tell your routing what to do (/app/condig/routes.php) with it
Router::connect('/user/*', array('controller' => 'users', 'action' => 'view'));
Now you will get pretty urls and the slug will be a parameter that can be used to find the user in your User table
Try this code
echo $this->Html->link('View Profile',array('plugin' => false,
'controller' => 'users',
'action' => 'view',
"slug"=>$data['slug']));
And in your Routes file
Router::connectNamed(array('slug'));
Router::connect('/user/:slug', array(
'plugin' => false,
'controller' => 'users',
'action' => 'view',
),array(
"pass"=>array("slug")
)
);

Routing: 'admin' => true vs 'prefix' => 'admin in CakePHP

Hi I'm setting up admin routing in CakePHP.
This is my current route:
Router::connect('/admin/:controller/:action/*', array('admin' => true, 'prefix' => 'admin', 'controller' => 'pages', 'action' => 'display', 'home'));
It works fine, but I don't understand what the difference between 'admin' => true, and 'prefix' => 'admin' is.
When I omitted 'prefix' => 'admin', the router wouldn't use admin_index and would instead just use index. So what's the point of 'admin' => true?
By setting 'prefix' => 'admin' you are telling CakePHP that you want to use a prefix of admin for that route; basically meaning you want to use controller actions and views that have names prefixed with admin_. This part you are already aware of, and things will probably work fine with just this.
When creating routes though, any array keys passed into the second argument that aren't recognised by CakePHP (ie. not your usual controller, action, plugin, prefix stuff) are set as named parameters during requests matching that route.
Adding 'admin' => true is therefore just a named parameter in this case, but it comes with its advantages. Firstly, it can make code more succinct.
/* Determine if a request came through admin routing */
// without:
if ($this->params['prefix'] == 'admin') {}
// with:
if ($this->params['admin']) {}
/* Create a link that is reverse-routed to an admin prefixed route */
// without:
$html->link('...', array('prefix' => 'admin', 'controller' => 'users'));
// with:
$html->link('...', array('admin' => true, 'controller' => 'users'));
Secondly, it provides backwards compatibility with the way admin routing worked in CakePHP 1.2 (the last line from the above example is how you would have made admin routing links in 1.2). Therefore, developers migrating from 1.2 to 1.3 can prevent having to change links throughout their application by keeping the 'admin' => true flag in their routes (and adding the 'prefix' => 'admin' one).
Lastly, by setting a custom flag like this with a named parameter and using it in your application instead of referencing your route by an exact string means that you prevent yourself from ever having to change links if you change the prefix to something else (say from admin to administrator or edit)... although this is sort of a moot point, as you would need to rename all your admin_* controller actions and views. :)
// Go into a prefixed route.
echo $html->link('Manage posts', array('manager' => true, 'controller' => 'posts', 'action' => 'add'));
// leave a prefix
echo $html->link('View Post', array('manager' => false, 'controller' => 'posts', 'action' => 'view', 5));

Resources