我注意到重定向控制器方法在部分时间内不起作用。设置调试>时不会显示任何消息0.在调用重定向方法之前,我没有回显任何代码,所以它不应该是因为“已经发送了标题”。
让我们来看看我的ArticlesController添加操作,其中重定向在一个实例中工作但在另一个实例中不工作。
public function add($page = null) {
// Custom component to get if user has required access level
// of page to write an article. If not, setflash to an error message
// specific to user's access level and redirect.
$access_message = $this->CustomPage->AccessMessage(4, $this->viewVars['access']);
if($access_message){
// Flash works but redirect does not
$this->Session->setFlash(__($access_message));
$this->redirect(array('action' => 'index', 'page' => $page));
// Also tried
// $this->redirect(array('controller'=>'articles', 'action' => 'index', 'page' => $page), null, true);
} else
{
if ($this->request->is('post')) {
$this->Article->create();
if ($this->Article->save($this->request->data)) {
// BLAH BLAH save post, do other stuff
// BLAH BLAH save post, do other stuff
// This flash and redirect works
$this->Session->setFlash(__('The article has been saved'));
$this->redirect(array('action' => 'view', 'id' => $article_id, 'page' => $page));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
} // end else if article cannot be saved
} // if method is post
} // end if user has access
} // end add action
它肯定与组件有关,但我不确定是什么。也许因为在使用组件后立即调用重定向,“$ this”试图在组件而不是控制器上执行重定向方法。我尝试了$ this-> Article->重定向并在重定向之前重新加载文章模型,但这些都没有。
我的组件代码是:
public function AccessMessage($required_level, $user_level) {
if(!$user_level && $this->_View->viewVars['access']){
$user_level = $this->_View->viewVars['access'];
}
if(!$required_level || !$user_level || $user_level != $required_level){
$accessModel = ClassRegistry::init('Access');
$access_message = $accessModel->field('access_message', array('Access.id' => $required_level));
}
return $access_message;
}
编辑1:好的,所以我做了一些挖掘,以找到问题的确切位置。组件的使用不是我以前认为的问题。如果我的组件中的所有内容都是
public function AccessMessage($required_level, $user_level) {
if(!$user_level && $this->_View->viewVars['access']){
$user_level = $this->_View->viewVars['access'];
}
if(!$required_level || !$user_level || $user_level != $required_level){
$access_message = 1;
}
return $access_message;
}
然后它有效。问题在于正确实现了这两行,因为它们返回了我期望的$ access_message的值,但是它们干扰了重定向的能力。也许标题已经发出了?
$accessModel = ClassRegistry::init('Access');
$access_message = $accessModel->field('access_message', array('Access.id' => $required_level));
请注意我也尝试过:
$access_message = ClassRegistry::init('Access')->field('access_message', array('Access.id' => $required_level));
和
$this->loadModel('Access');
$access_message = $this->Access->field('access_message', array('Access.id' => $required_level));
要旨:
组件要点:https://gist.github.com/970a951715205c222348
控制器要点:https://gist.github.com/2b90e5af2518a81672fb
访问模型要点:https://gist.github.com/bhndbrwneyes/f333a93f0a21302d832f
答案 0 :(得分:2)
您可能有空格before/after php Opening/Closing tags in controller and models
。在打开标签之前,从所有控制器和模型以及任何空格中删除所有结束标签。然后检查结果。
答案 1 :(得分:1)
还没有答案,但至少在您的代码中有一些可能未定义的变量或可能发生的错误:
public function AccessMessage($required_level, $user_level) {
if(!$user_level && $this->_View->viewVars['access']){
$user_level = $this->_View->viewVars['access'];
}
if(!$required_level || !$user_level || $user_level != $required_level){
$access_message = 1;
}
return $access_message;
}
将其更改为:
public function AccessMessage($required_level, $user_level) {
$access_message = 0;
if(!$user_level && $this->_View->get('access')){
$user_level = $this->_View->get('access');
}
if(!$required_level || !$user_level || $user_level != $required_level){
$access_message = 1;
}
return $access_message;
}
[已更新]看到已在您的要点上定义了$ access_message(https://gist.github.com/970a951715205c222348)
然而: 这将不工作
App::uses('Component', 'Controller', 'ClassRegistry', 'Utility');
App :: uses()接受两个参数;您想要使用的“课程”以及可以找到的位置。上面的行应该写成:
App::uses('Component', 'Controller/Component');
App::uses('Controller', 'Controller');
App::uses('ClassRegistry', 'Utility');
但我想知道是否需要手动加载ClassRegistry
[更新2]你的应用程序中确实发生了很多“奇怪”的事情,所以我想知道我们是否能够解决这个问题:
public function add($page = null) {
$access = $this->viewVars['access'];
if($this->CustomPage->AccessMessage(4, $access)){
$this->Session->setFlash(__($this->CustomPage->AccessMessage(4, $access)));
$this->redirect(array('action' => 'index', 'page' => $page));
}
// ......
}
public function add($page = null) {
// Where does is $this->viewVars['access'] come from? Where is it set?
$access = empty($this->viewVars['access'])? null : $this->viewVars['access'];
$message = $this->CustomPage->AccessMessage(4, $access);
if ($message) {
$this->Session->setFlash(__($message));
$this->redirect(array('action' => 'index', 'page' => $page));
}
// ......
}
进一步说明。 只有在找到“消息”并且不是空的时候重定向用户,不是基于当前用户权限,您可以考虑拆分这两者;
在您的组件中:
public function HasAccessLevel($required_level, $user_level) {
if(!$user_level || $user_level != $required_level){
return false;
}
return true;
}
public function AccessMessage($required_level) {
return ClassRegistry::init('Access')->field('access_message', array('Access.id' => $required_level));
}
在您的控制器中:
public function add($page = null) {
// Where does is $this->viewVars['access'] come from? Where is it set?
$access = empty($this->viewVars['access'])? null : $this->viewVars['access'];
if($this->CustomPage->HasAccessLevel(4, $access)){
$this->Session->setFlash(__($this->CustomPage->AccessMessage(4)));
$this->redirect(array('action' => 'index', 'page' => $page));
}
// ......
}
答案 2 :(得分:1)
蛋糕2:在组件的顶部:添加
public function initialize(Controller $controller) {
$this->Controller = $controller;
}
在函数内部,您可以像这样重定向:
$this->Controller->redirect(array('plugin' => false, 'controller' => 'users', 'action' => 'index'));
答案 3 :(得分:0)
如果调用了重定向,但未重定向,我猜您在index()
操作中有一些权限可以阻止访问。你能确认或发布整个控制器代码吗?