简体   繁体   中英

Where should I put the returned text action or javascript?

In Symfony 1.4, if I want to send some text to render through an AJAX request, where should I put text

  • in the action and send it to Javascript to render
  • put the text in Javascript

I want to know the best practice.

Example 1

PHP action

class defaultActions extends sfActions
{
    public function executeSomething(sfWebRequest $request)
    {
        $status = 'valid';
        if($status == 'valid'){
                return $this->renderText('valid');
        }
        else{
                return $this->renderText('invalid');
        }
    }
}

JS

if (valid)  
   fun1();
else
   fun2();

I would call the controller as normal and then do whatever processing is required in the action.class.php file and then return the text required. So my action.class.php file may well be as simple as :

class defaultActions extends sfActions
{
    public function executeSomething(sfWebRequest $request)
    {
        $sometext = "return value";
        return $this->renderText($sometext);
    }
}

There are a number of advantages

  • Security - can be restricted
  • Refactoring - creating the correct action means easier changes should you need to get data from a model or display something other than text (JSON or XML via the view)
  • Testing - it allows easy testing using the normal function / unit tests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM