# Why param disapperer from url after save a post and error? Cake php 2

**URL:** <https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207>\
**Category:** Need Help\
**Created:** [March 6, 2022, 11:54pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207 "2022-03-06T23:54:40Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [March 6, 2022, 11:54pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/1 "2022-03-06T23:54:40Z")

</div>

I would like to know the reason why the param pass (in this case the post id) is lost and this error is returned. I’m going crazy with so many things I’ve tried and it doesn’t work, and I’m feeling like it’s some simple mistake…

The error:

```auto
Notice (8): Undefined offset: 0 [APP/Controller/PostsController.php, line 65]
Code Context
            // O dono de um post pode editá-lo e deletá-lo

            if (in_array($this->request->action, array('edit', 'delete'))) {

                $postId = (int) $this->request->params['pass'][0];
$user = array(
	'id' => (int) 48,
	'username' => 'adm',
	'role' => 'admin',
	'created' => '2022-01-20 15:28:24',
	'modified' => '2022-01-20 15:28:24'
)
PostsController::isAuthorized() - APP/Controller/PostsController.php, line 65
ControllerAuthorize::authorize() - CORE/Cake/Controller/Component/Auth/ControllerAuthorize.php, line 63
AuthComponent::isAuthorized() - CORE/Cake/Controller/Component/AuthComponent.php, line 470
AuthComponent::startup() - CORE/Cake/Controller/Component/AuthComponent.php, line 311
ObjectCollection::trigger() - CORE/Cake/Utility/ObjectCollection.php, line 129
CakeEventManager::dispatch() - CORE/Cake/Event/CakeEventManager.php, line 244
Controller::startupProcess() - CORE/Cake/Controller/Controller.php, line 683
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 189
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 167
[main] - APP/webroot/index.php, line 117

```

My Post controller and view, respectively:

```auto

<?php
// File: /app/Controller/PostsController.php

App::uses('AppController', 'Controller');

class PostsController extends AppController {
    public $helpers = array ('Html','Form');
    public $name = 'Posts';

    public function index(){
        //$this->autoRender = false;

    }
    public function list() {
        $this->set('list', $this->Post->find('all'));
    }

    public function view($id = null) {
        $this->set('post', $this->Post->findById($id));
    }
    public function add() {
        if ($this->request->is('post')) {
            $this->request->data['Post']['user_id'] = $this->Auth->user('id'); // Adicionada essa linha
            if ($this->Post->save($this->request->data)) {
                $this->Flash->success('Your post has been saved.');
                $this->redirect(array('action' => 'list'));
            }
        }
    }

    public function edit($id = null) {
        
        // Campo do id recebe id do post atual
        $this->Post->id = $id;
        if ($this->request->is('get')) {
            // var_dump($this->request->data);
            $this->request->data = $this->Post->findById($id);
        } else {
            // var_dump($this->request->data);
            if ($this->Post->save($this->request->data)) {
                $this->Flash->success('Seu Post foi atualizado.');
                $this->redirect(array('action' => 'list'));
            }
        }
    }

    public function delete($id) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        if ($this->Post->delete($id)) {
            $this->Flash->success('O post com o id: ' . $id . ' foi excluído.');
            $this->redirect(array('action' => 'list'));
        }
    }

    public function isAuthorized($user) {
        if (parent::isAuthorized($user)){
            // Todos os usuários registrados podem criar posts
            if ($this->action === 'add') {
                return true;
            }
            // O dono de um post pode editá-lo e deletá-lo
            if (in_array($this->request->action, array('edit', 'delete'))) {
                $postId = (int) $this->request->params['pass'][0];
                return $this->Post->isOwnedBy($postId, $user['id']);
            }
        }
    }
}
?>

```

```auto
<!-- File: /app/View/Posts/edit.ctp -->

<h1>Edit Post</h1>
<?php
    echo $this->Form->create('Post', array('url' => 'edit'));
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '3'));
    echo $this->Form->input('id', array('type' => 'hidden'));
    echo $this->Form->end('Save Post');

```

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [March 7, 2022, 2:50pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/2 "2022-03-07T14:50:50Z")

</div>

You get that notice because `$this->request->params['pass']` is empty at that spot.  
So you should check

```auto
if(isset($this->request->params['pass'][0]){

}

```

I can’t tell your how/where that `pass` request param should be set (especially in CakePHP 2) so I guess you expect `pass` to always have some value even though it doesn’ t.

---

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [March 7, 2022, 5:30pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/3 "2022-03-07T17:30:16Z")

</div>

Thanks for reply KevinPfeifer,

The param is passed in url when i click on link of edit posts, the post id is passed as param, i have access to edit title and body of post usually, even alterations are saved, but this error yet ocurrs.

Url example: [http://localhost:8888/posts/edit/27](http://localhost:8888/posts/edit/27)  
“27” is the id of a post

---

<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:** [March 7, 2022, 6:23pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/4 "2022-03-07T18:23:33Z")

</div>

What does `debug($this->request->params)` give you if you try it right before the line that gives the error?

---

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [March 7, 2022, 8:10pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/5 "2022-03-07T20:10:39Z")

</div>

Thanks for reply, Zuluru

Debug returns the following before save the post:

```auto
/Controller/PostsController.php (line 65)
array(
	'plugin' => null,
	'controller' => 'posts',
	'action' => 'edit',
	'named' => array(),
	'pass' => array(
		(int) 0 => '27'
	)
)

```

And the following after save the post:

```auto
/Controller/PostsController.php (line 65)
array(
	'plugin' => null,
	'controller' => 'posts',
	'action' => 'edit',
	'named' => array(),
	'pass' => array()
)

```

---

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [March 7, 2022, 8:15pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/6 "2022-03-07T20:15:58Z")

</div>

If help, here is the edit view code:

```auto
<!-- File: /app/View/Posts/edit.ctp -->

<h1>Edit Post</h1>
<?php
    echo $this->Form->create('Post', array('url' => 'edit'));
    echo $this->Form->input('title');
    echo $this->Form->input('body', array('rows' => '3'));
    echo $this->Form->input('id', array('type' => 'hidden'));
    echo $this->Form->end('Save Post');

```

---

<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:** [March 7, 2022, 10:46pm UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/7 "2022-03-07T22:46:08Z")

</div>

> [@bigcouve](#):
>
> `echo $this->Form->create('Post', array('url' => 'edit'));`

Here’s your issue. You are telling it that you want to post to a URL that does not include your ID in it.

---

<div class="post-metadata">

**Author:** ![bigcouve](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@bigcouve](https://discourse.cakephp.org/u/bigcouve)\
**Post date:** [March 8, 2022, 1:27am UTC](https://discourse.cakephp.org/t/why-param-disapperer-from-url-after-save-a-post-and-error-cake-php-2/10207/8 "2022-03-08T01:27:22Z")

</div>

Thank you so much Zuluru s2, I was looking the solution in the middle and ending of logical, even learn about add url but i don’t searched in edit view. I lost around one week trying solve this problem ☹ , but thanks to your help I will also fix the following problems at the beginning of the streams ^^
