开发者

CakePHP passing id to edit form

I've noticed that their is many different ways to pass an ID to a form when editing a database entry. So for example for a edit user profile form I have the following code:

function edit($id = null)
{
    $this->layout = 'page';

    // this line isn't needed?
    //$this->User->id = $id;

    if (empty($this->data))
    {
        $this->data = $this->User->read();
    }
    else
    {
        if ($this->User->save($this->data))
        {
            $this->Session->setFlash('Your profile has been updated', 'flash', array('header' => 'Announcement', 'myclass' => 'success'));
            $this->redirect(array('controller' => 'users', 'action' => 'view', $id));
        }
    }
}

Now the function expects an id passing in the url e.g. /users/edit/2 But let's say I wanted it to be something more user friendly like /profile/edit (rewrote by routing) I would no longer be passing in the ID as part of the url. As you can see in my code I have a line I have commented out because it isn't needed?

Also in the form I ALSO Need <?php echo $this->Form->input('id', array('type' => 'hidden')); ?> why?

Basically this is more of what are the options available to me to build various types of edit forms and passing data to the form. And what is the need for the hidden field in the form if the data is being passed either via the URL or some other way

I've also noticed on some sites that they have things like Form Keys and the username stored in meta tags in the page header???

EDIT:

public function beforeFilter()
{

        $this->set('authUser', $this->Auth->user());

        //

        $user = $this->Auth->user();

        if (!empty($user))
        {
            Configure::write('User', $user[$this->Auth->getModel()->alias]);
        }

}

public function beforeRender()
{
    $user = $this->Auth->user();

    if (!empty($user))
    {
        $user = $user[$this->Auth->getModel()->alias];
    }
    $this->set(compact('user'));
}


// NEW VERSION
function settings()
    {
        $this->layout =开发者_如何学编程 'page';

        $this->set('title_for_layout', 'Edit Profile');

        $this->User->id = $user['id'];

        if (empty($this->data))
        {
            $this->data = $this->User->read();
        }
        else
        {
            if ($this->User->save($this->data))
            {
                $this->Session->setFlash('Your settings have been updated', 'flash', array('myclass' => 'success'));
                $this->redirect(array('controller' => 'users', 'action' => 'settings'));
            }
        }
    }


Also in the form I ALSO Need Form->input('id', array('type' => 'hidden')); ?> why?

Having the id hidden in the form removes the need for your controller action to grab the $id from the uri (aka passed as parameter). When in the form, it will automatically be placed into your $data array.

what is the need for the hidden field in the form if the data is being passed either via the URL or some other way

It's not needed in the form if it's available from the uri. You'd simply grab the $id and assign it to the User model (as the commented out code does).

let's say I wanted it to be something more user friendly like /profile/edit

I assume that would be when the user is editing his own profile. In that case, your system should be able to retrieve the user's id via the session.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜