# Cakephp 3.6 : Extract a value from the url

**URL:** <https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517>\
**Category:** Need Help\
**Created:** [January 12, 2019, 1:45pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517 "2019-01-12T13:45:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![LALPAGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/lalpaga/32/1204_2.png) [@LALPAGA](https://discourse.cakephp.org/u/LALPAGA)\
**Post date:** [January 12, 2019, 1:45pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/1 "2019-01-12T13:45:07Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 12, 2019, 6:40pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/2 "2019-01-12T18:40:52Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![LALPAGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/lalpaga/32/1204_2.png) [@LALPAGA](https://discourse.cakephp.org/u/LALPAGA)\
**Post date:** [January 12, 2019, 11:23pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/3 "2019-01-12T23:23:06Z")

</div>

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  
 ![43342a6bd94cccd16d599af2f400c853](https://cdck-file-uploads-canada1.s3.dualstack.ca-central-1.amazonaws.com/flex029/uploads/cakephp/original/2X/e/e2d4b03d9326458803b3076662d2118e9df86f1d.png)

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 13, 2019, 9:29pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/4 "2019-01-13T21:29:58Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LALPAGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/lalpaga/32/1204_2.png) [@LALPAGA](https://discourse.cakephp.org/u/LALPAGA)\
**Post date:** [January 13, 2019, 9:38pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/5 "2019-01-13T21:38:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LALPAGA](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/lalpaga/32/1204_2.png) [@LALPAGA](https://discourse.cakephp.org/u/LALPAGA)\
**Post date:** [January 13, 2019, 9:51pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/6 "2019-01-13T21:51:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Speudyland](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/speudyland/32/569_2.png) [@Speudyland](https://discourse.cakephp.org/u/Speudyland)\
**Post date:** [January 16, 2019, 4:13pm UTC](https://discourse.cakephp.org/t/cakephp-3-6-extract-a-value-from-the-url/5517/7 "2019-01-16T16:13:12Z")

</div>

Otherwise you can use php function [parse\_url](http://php.net/manual/en/function.parse-url.php) 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));
```
