view problem

This is my first post here and just starting with mvc and yii.

I have this code in the controller , Crud generated.

I’ve created a new method and a new view.

the method code:

public function actionConfirmacao($id)

    {


         $this->render('confirmacao',array(


        'model'=>$this->loadModel($id),


	));


         


    }

The ‘confirmacao’ view code is the same as the ‘view’ view

Acessrules to confirmacao method is allow all but still the 400 page

Your request is invalid error

Thanks in advance.

Could you please post more info? Your View action is working fine? It’s generated by Gii? Could you please post the full controller source? And also post the URL used in the request.

View action is working fine and generated by Gii.

url is index.php/proprietario/confirmacao/

<?php

class ProprietarioController extends Controller

{

private &#036;_identity;


/**


 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning


 * using two-column layout. See 'protected/views/layouts/column2.php'.


 */


public &#036;layout='//layouts/column2';


   





    /**


 * @return array action filters


 */


public function filters()


{


	return array(


		'accessControl', // perform access control for CRUD operations


	);


}





/**


 * Specifies the access control rules.


 * This method is used by the 'accessControl' filter.


 * @return array access control rules


 */


public function accessRules()


{


	


                


            return array(


                    array('allow',  // allow all users to perform 'index' and 'view' actions


                        // need to allow captcha in order to view it.


                        'actions'=&gt;array('index','view','confirmacao','captcha'),


                        'users'=&gt;array('*'),


                    ),


		


		array('allow', // allow authenticated user to perform 'create' and 'update' actions


			'actions'=&gt;array('create','update'),


			'users'=&gt;array('@'),


		),


		array('allow', // allow admin user to perform 'admin' and 'delete' actions


			'actions'=&gt;array('admin','delete'),


			'users'=&gt;array('admin'),


		),


		


	);


}


    


    public function actionConfirmacao(&#036;id)


    {


         


	&#036;this-&gt;render('confirmacao',array(


		'model'=&gt;&#036;this-&gt;loadModel(&#036;id),


	));


         


    }





    /**


 * Displays a particular model.


 * @param integer &#036;id the ID of the model to be displayed


 */


public function actionView(&#036;id)


{


	&#036;this-&gt;render('view',array(


		'model'=&gt;&#036;this-&gt;loadModel(&#036;id),


	));


}





/**


 * Creates a new model.


 * If creation is successful, the browser will be redirected to the 'view' page.


 */


public function actionCreate()


{


	&#036;model=new Proprietario;





	// Uncomment the following line if AJAX validation is needed


	&#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Proprietario']))


                


	{


                


		&#036;model-&gt;attributes=&#036;_POST['Proprietario'];


		if(&#036;model-&gt;save())


                      &#036;this-&gt;redirect(array('confirmacao','id'=&gt;&#036;model-&gt;propid));


			//&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;propid));


	}





	&#036;this-&gt;render('_form',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Updates a particular model.


 * If update is successful, the browser will be redirected to the 'view' page.


 * @param integer &#036;id the ID of the model to be updated


 */


public function actionUpdate(&#036;id)


{


	&#036;model=&#036;this-&gt;loadModel(&#036;id);





	// Uncomment the following line if AJAX validation is needed


	// &#036;this-&gt;performAjaxValidation(&#036;model);





	if(isset(&#036;_POST['Proprietario']))


	{


		&#036;model-&gt;attributes=&#036;_POST['Proprietario'];


		if(&#036;model-&gt;save())


			&#036;this-&gt;redirect(array('view','id'=&gt;&#036;model-&gt;propid));


	}





	&#036;this-&gt;render('update',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Deletes a particular model.


 * If deletion is successful, the browser will be redirected to the 'admin' page.


 * @param integer &#036;id the ID of the model to be deleted


 */


public function actionDelete(&#036;id)


{


	if(Yii::app()-&gt;request-&gt;isPostRequest)


	{


		// we only allow deletion via POST request


		&#036;this-&gt;loadModel(&#036;id)-&gt;delete();





		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser


		if(&#33;isset(&#036;_GET['ajax']))


			&#036;this-&gt;redirect(isset(&#036;_POST['returnUrl']) ? &#036;_POST['returnUrl'] : array('admin'));


	}


	else


		throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');


}





/**


 * Lists all models.


 */


public function actionIndex()


{


	&#036;dataProvider=new CActiveDataProvider('Proprietario');


	&#036;this-&gt;render('index',array(


		'dataProvider'=&gt;&#036;dataProvider,


	));


}





/**


 * Manages all models.


 */


public function actionAdmin()


{


	&#036;model=new Proprietario('search');


	&#036;model-&gt;unsetAttributes();  // clear any default values


	if(isset(&#036;_GET['Proprietario']))


		&#036;model-&gt;attributes=&#036;_GET['Proprietario'];





	&#036;this-&gt;render('admin',array(


		'model'=&gt;&#036;model,


	));


}





/**


 * Returns the data model based on the primary key given in the GET variable.


 * If the data model is not found, an HTTP exception will be raised.


 * @param integer the ID of the model to be loaded


 */


public function loadModel(&#036;id)


{


	&#036;model=Proprietario::model()-&gt;findByPk(&#036;id);


	if(&#036;model===null)


		throw new CHttpException(404,'The requested page does not exist.');


	return &#036;model;


}





/**


 * Performs the AJAX validation.


 * @param CModel the model to be validated


 */


protected function performAjaxValidation(&#036;model)


{


	if(isset(&#036;_POST['ajax']) &amp;&amp; &#036;_POST['ajax']==='proprietario-form')


	{


		echo CActiveForm::validate(&#036;model);


		Yii::app()-&gt;end();


	}


}


    


    public function actions()


        {


            return array(


                // captcha action renders the CAPTCHA image displayed on the register page


                'captcha'=&gt;array(


                    'class'=&gt;'CCaptchaAction',


                    'backColor'=&gt;0xFFFFFF,


                ),


            );


        }

}

Error 400 = invalid request.

This has happened because of mandatory ‘id’ param in actionConfirmacao($id)

Provide id by using URL like /proprietario/confirmacao/1 and everything will be fine :)

PS. In some actions you may want to make parameters optional.

Here’s the example of code with optional param:




public function actionEdit($id = null)

{

    if ($id) {

        $model = Example::model()->findByPk($id);

    } else {

        $model = new Example;

    }


    // ...

}



BTW, read the docs about YII action params handling, it’s really cool.