Cakephp 3.6 : Extract a value from the url


#1

Hello guys, i struggle to extract a value from the URL of a page here is the URL :
http://localhost/projet-1a2a-gestion-des-fiches-de-suivi-licence-pro/cakephp/users/editpass/15

i want to extract the very laster number (15) in the function isAuthorized so i can unothorized users to no access pages to edit other’s password.

Thanks in advance


#2

What does the signature of your editpass function look like? Unless you’ve done something weird with routing, you should be able to just use function editpass($id) and the 15 will be in the $id variable.


#3

Here is my editpass function, it allows a user to change its password and username with a form.
I made i like the default edit function of the UsersController and as you can see i did let the $id = null


#4

My apologies, I missed the part about isAuthorized. Take a look at $this->request->params['pass'].


#5

I got it with
$this->request->params[‘pass’][0];
(i added [0] because it’s an array).
Thank you very much !


#6

Here is my final isAutorized function :

public function isAuthorized($user){
   if($this->request->getParam('action') ==='editpass' && $this->request->getParam('pass')[0] === (string)$this->Auth->user('id'))
       return true;
   else
       return parent::isAuthorized($user);
}

#7

Otherwise you can use php function parse_url and get last key of your array (it’s also good to check if it’s is_numeric or not)

$last_key = key(array_slice($array, -1, 1, TRUE));