1
votes

I have a function that is used to edit a previously existing userprofile here is the controller

class PeoplesController extends AppController
{
    public $name = "peoples";
    public $helpers = array('Html', 'form', 'Js');
    public $components = array('RequestHandler');

     public function viewPerson($id = NULL) {
        $this->set('person', $this->people->read(NULL, $id));
    }

    public function editPerson($id = NULL)
    {
        if(empty($this->data)) {
            $this->data = $this->people->read(NULL, $id);
        }
        else {
            if($this->people->save($this->data)) {
                $this->Session-setFlash('The profile has been updated');
                $this->redirect(array('action'=>'viewPerson', $id));
            }
        }

    }
}   

the view outputs a form prepopulated with the previous data

<h2>Edit Profile</h2>

 <?php 

  echo $this->Form->create('people', array('action'=>'edit'));

echo $this->Form->input('firstName');
echo $this->Form->input('secondName');
echo $this->Form->input('addressOne');
echo $this->Form->input('addressTwo');
echo $this->Form->input('city');
echo $this->Form->input('county');
echo $this->Form->input('country');
echo $this->Form->input('postCode', array(
        'label' => 'Zip Code',
    ));
echo $this->Form->input('dob', array(
        'label' => 'Date of birth',
        'dateFormat' => 'DMY',
        'minYear' => date('Y') - 70,
        'maxYear' => date('Y') - 18,
    ));
echo $this->Form->input('homePhone');
echo $this->Form->input('mobilePhone');
echo $this->Form->input('email', array(
        'type' => 'email'
    ));

$goptions = array(1 => 'Male', 2 => 'Female');
$gattributes = array('legend' => false);

echo $this->Form->radio('gender', 
    $goptions, $gattributes
    );

echo $this->Form->input('weight');
echo $this->Form->input('height');

echo $this->Form->input('referedBy');

echo $this->Form->input('id', array('type'=>'hidden'));

echo $this->Form->end('Edit Profile');
    ?>

the view seems to work fine @

http://localhost/Cake/peoples/editPerson/2

but when I hit submit the url changes to

http://localhost/Cake/people/edit/2

So I changed

echo $this->Form->create('people', array('action'=>'edit'));

to

echo $this->Form->create('people', array('action'=>'editPerson'));

and I tried changing the first parameter of the create function to peoples but as this refers to the model and not the controller it should be people so i changed it back

the error i get is

Error: PeopleController could not be found.

so it is looking for the wrong controller but I am not sure where that seeking for the controller takes place.

I did use to have the controller class name uncapitalised but I follwed some advice on the conventions and capitalised it but the other previous functions I had still work fine so I dont think that is the problem

any ideas why it is changing the url and looking for the wrong controller?

1
When Iw as getting started I had created the tables before I read about cakes conventions so I just went with it. Someone else has suggested to me that this could be the problem and if it is great but if that is the case I am wondering why the other functions work ok?Mark

1 Answers

2
votes

The form action is being set to the edit action

Irrespective of the current url - the form action is overridden to be an explicit action:

echo $this->Form->create(
    'people', 
    array('action'=>'edit') // <-
);

This will force the url to point at the action edit, or /people/edit/<current id>.

To prevent that, just don't specify the action, and the form will submit to the current url:

echo $this->Form->create(
    'people'
);

To specify the form action url

Use the url key, not the action key which only changes the action:

echo $this->Form->create(
    'people', 
    array(
        'url'=>'/submit/here/please/' // <- or specify as an array
    )
);

Avoid/correct confusing names

The convention regarding model and controller names is that models are singular, and controllers are plural:

Model    |   Controller
Singular     Plural
Person       People

In the question this isn't the case, The model is a plural, and the controller is a confusing plural-plural (Peoples can actually be used, but it means many groups of people)

Don't define properties needlessly

In the question, there's also this:

class PeoplesController extends AppController
{
     public $name = "peoples";

At best that doesn't do anything, but since the name property is defined as lower case and cake expects it to be CamelCased - that's likely to cause inconsistent behavior.

Best to follow conventions and name the controller People, the model Person and don't specify a name at all.

Don't repeat yourself

And follow conventions for a much easier ride.

The function naming is duplicates the word "person"

instead of:

public function viewPerson($id = NULL) {

(so.. the code uses person, people and peoples ?)

it's advisable to use:

public function view($id = NULL) {

After all, in the people controller - what other kind of thing is there to view (if the answer is many things, that's another thing to address)?

This makes urls simple (/peoples/view/1, instead of /peoples/viewPeople/1) as well as making the code itself easier to read (PeoplesController::view instead of PeoplesController::viewPerson).

If you don't want to change the names of the action methods, making them lower cased and underscored will at least mean the urls are consistently cased, instead of /lower_case_controller/camelCasedAction/args.