在symfony中重构代码

时间:2011-09-04 23:25:06

标签: php symfony1 symfony-1.4

if (!$this->getUser()->isAuthenticated())
{
  $this->redirect('login/index');
}

我想为不同模块中的许多操作添加此代码。我必须为每个动作复制此代码吗?我想做点事情:

//module 

class Invite
public function executeIndex {
 sfConfig:get('redirect');
}


class Home
public function executeIndex {
 sfConfig:get('redirect');
}

public function executeShow {
  sfConfig:get('redirect');
}

如果不能在不同的模块中,虽然它可能在一个单独的?

2 个答案:

答案 0 :(得分:1)

您需要的是过滤器。 See here

答案 1 :(得分:1)

您可以使用symfony的内置安全过滤器。

如果您想保护整个模块,请使用以下内容创建apps / app / modules / module / config / security.yml:

all:
  is_secure: true

如果您只想让show动作安全:

show:
  is_secure: true

请注意,您只需要操作的名称,不包括结果(例如,成功)。

你可以只使用内置的安全层做很多事情,甚至可以使用像sf(Doctrine)GuardPlugin这样的东西。有关详细信息,请参阅Gentle introduction to symfony, chapter 6, action security