Convert Actions To Web Service ?

Hi

Is it possible to Convert actions to Web service ? I want to "DRY" and save time

for example , assume this code




        /**

	 * Creates a new model.

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

	 */

	public function actionCreate()

	{

		$model=new Post;

		if(isset($_POST['Post']))

		{

			$model->attributes=$_POST['Post'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}



if we reflect to




	 public function actions()

	     {

 	        return array(

 	            'api'=>array(

	                 'class'=>'CWebServiceAction',

	             ),

	         );

	     }


         /**

	 * Creates a new model.

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

	 * @soap

	 */

	public function actionCreate()

	{

		$model=new Post;

		if(isset($_POST['Post']))

		{

			$model->attributes=$_POST['Post'];

			if($model->save())

				$this->redirect(array('view','id'=>$model->id));

		}


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

			'model'=>$model,

		));

	}



how we can $_POST in web service client? it is possible ? I want to don’t create actionCreate twice , one for usually usage and one for web service client

Yii has pretty docs that covers Web Services in depth here is a link

http://www.yiiframework.com/doc/guide/1.1/en/topics.webservice

Yes , I know and as you can see in reflected code I do

My Problem is , I have an action like this




        public function actionTest()

        {

                input $_POST and validation

                do something and throw CException in errors

                render Output

        }




I don’t want make another method for webservice for example





         public function actions()

             {

                return array(

                    'api'=>array(

                         'class'=>'CWebServiceAction',

                     ),

                 );

             }


     /**

     * @param array Input

     * @return string Output

     * @soap

     */

    public function getTest($apiInput)

    {

        input $apiInput and validation

        do something and throw SoapFault in errors

        return Output

    }




that means , my actions and web service do same , and just a little difference that shown in following

Action … Web Service

input is $_POST … input is Argument

throw CException … throw SoapFault

render Output … return output

and other programming line is same

so I don’t want program action , copy it and past in web service method and change some lines

I want create ONE action and my action do both , "Usually action" and "web service"

for example




 public function actions()

             {

                return array(

                    'api'=>array(

                         'class'=>'CWebServiceAction',

                     ),

                 );

             }


         /**

         * Creates a new model.

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

         * @soap

         */

        public function actionCreate()

        {

                $model=new Post;

                if(isset($_POST['Post']))

                {

                        $model->attributes=$_POST['Post'];

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }


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

                        'model'=>$model,

                ));

        }



but problem is , if I user $_POST I cannot $_POST in soap client

If I use "public function actionCreate($inputs)" , I have problem in action

if I use





         /**

         * Creates a new model.

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

         * @param array Soap Input

         * @return boolean Is Saved ?

         * @soap

         */

        public function actionCreate($soapInput=null)

        {

                

                $isSoap = isset($soapInput);

                $model=new Post;

                if(isset($_POST['Post'],$soapInput))

                {

                        $model->attributes=$isSoap?$soapInput:$_POST['Post'];

                        if($isSoap)

                                return $model->save();

                        else if($model->save())

                           $this->redirect(array('view','id'=>$model->id));


                }

                if(!$isSoap)

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

                             'model'=>$model,

                     ));

        }




It works but it is very hard coded and I think Yii must have better way

I hope that you understand what I need

:rolleyes: Reply please

Hi,

I think you need to write your application logic inside the model itself and call it from anywhere you need it, either from web controller actions or web service controller.

Assume user creation

User.php // model

public function createUser() {

// Do all the stuff that related to user creation

$this->save();

}

UserController.php // Controller

public function actionCreate() {

if (isset($_POST[‘User’])) {

  $model->attributes = $_POST['User'];


  $model->createUser();

}

}

ApiController.php // Websevice or REST api

public function actionCreate() {

$model->attributes = <POST/GET attributes>;

$model->createUser();

}

Thanks

Aruna

Hi Aruna Attanayake, and thank you for your reply

Yes , You think right

But what about exceptions?




User.php // model


 public function createUser() {

 // Do all the stuff that related to user creation

 ..

//When Here I want throw Exceptions

 ..

 $this->save();

 ..

 ..

 }



Or assume I have Throw exception in BeforeSave() method

As you know in API I must throw Fault and in web controller actions throw CExeption

Yes In that case you have to separately address these scenarios on respective controller action. Another thing to consider is flash messages.

Currently I too working on such situation and I`ll keep posted if I found better way.

OK , thanks a lot ;)

I don’t think moving controller logic to model is a good idea. I understand your point that will end up with long actions but its perfectly fine

I do this in my application but it is not good solution and it is hard coded

then you should actually look at yii2 has some pretty good support for different response format

ok

thanks