小编Boh*_*vel的帖子

Symfony2:每次操作后执行一些代码

我最近在Symfony2中启动了一个项目,我需要在每个操作之前和之后运行一些方法以避免代码冗余(例如来自Zend Framework的preDispatch/postDispatch和来自Symfony1的PreExecute/PostExecute).

我创建了一个继承所有控制器的基类,并在运行请求的操作之前注册了一个事件监听器来运行控制器的preExecute()方法,但是在阅读了大量的文档和问题后,我仍然无法找到如何运行postExecute ().

美孚/ BarBundle /控制器/ BaseController.php:

class BaseController extends Controller {
  protected $_user;
  protected $_em;

  public function preExecute() {
    $user = $this->get('security.context')->getToken()->getUser();
    $this->_user = $user instanceof User ? $user : null;
    $this->_em = $this->getDoctrine()->getEntityManager();
  }

  public function postExecute() {
    $this->_em->flush();
  }
}
Run Code Online (Sandbox Code Playgroud)

美孚/ BarBundle /控制器/ FooController.php:

class FooController extends BaseController {
  public function indexAction() {
    $this->_user->setName('Eric');
    $this->_em->persist($this->_user);
  }
}
Run Code Online (Sandbox Code Playgroud)

美孚/ BarBundle /事件监听/ PreExecute.php:

class PreExecute {
  public function onKernelController(FilterControllerEvent $event) {
    if (HttpKernelInterface::MASTER_REQUEST === $event->getRequestType()) {
      $controllers = …
Run Code Online (Sandbox Code Playgroud)

php symfony

9
推荐指数
1
解决办法
5037
查看次数

为什么两个字符串看起来相同,但它们不是?

我的PHP代码:

$hello = "Hello, world! ";
echo $string1 = sprintf("%'#-20s\n", $hello); // Displays "Hello, world! ######"
echo $string2 = str_pad($hello, 20, "#");     // Displays "Hello, world! ######"
echo ($string1 == $string2) ? "Indeed they're equal" : "They're not equal";
                                              //  Displays "They're not equal"
echo strcmp($string1, $string2); // Displays "-1", which (according to PHP Manual)
                                 // means that $string1 is less than $string2
Run Code Online (Sandbox Code Playgroud)

字符串$string1$string2不相等的原因是什么?

php string-comparison

1
推荐指数
1
解决办法
331
查看次数

标签 统计

php ×2

string-comparison ×1

symfony ×1