为什么zendframework的_forward方法能用而_redirect方法却不能用?

yinludrink 2011-11-17 07:30:49
我一在某个Action中写入这条代码:
$this->_redirect('/login/success');
就会产生这样的错误:
Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in
而我改为:
$this->_forward("success","Login");就能够运行。


奇怪的是,我在地址栏中输入“http://localhost/mrzf/public/Login/success”是可以显示页面的,我的根目录是mrzf。
但是我在IndexController中的一个Action中,写入此条语句$this->_redirect('/Login/success');
就会出现'Invalid controller specified (error)'

补充说明一下,我的首页是一个登录框,用POST传递,先指向IndexController中的loginAction,然后在里面用$this->_redirect转向LoginController的successAction



我在:c:\AppServ\www\mrzf\application\views\scripts\index文件夹建一个login.phtml文件,随便写点代码。

然后把IndexController中的loginAction方法中的代码$this->_redirect('/Login/success');注释掉,添加$this->view->assign('title','zend framework框架测试成功!');

然后往浏览器的地址栏输入http://localhost/mrzf/public/index/login,是可以找到控制器和Action的,能够显示页面
但如果改回来,$this->_redirect('/Login/success');就会出错,'Invalid controller specified (error)',不知为什么?
...全文
423 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zend_ml 2011-12-15
  • 打赏
  • 举报
回复
我试过了,我是有单模块的,两个action 都在indexController 下是没问题的
public function ttAction(){
$this->_redirect('/index/tc');
}

public function tcAction(){
die("1243");
}
ohmygirl 2011-11-21
  • 打赏
  • 举报
回复
你的zendframework添加多模块了?
这些参数应该是设置多模块的。
yinludrink 2011-11-19
  • 打赏
  • 举报
回复
这个问题也不知怎么搞的,
自己在application.ini中添加了
[project]
bootstrap.path="../application/Bootstrap.php"
bootstrap.class="Bootstrap"
phpSettings.display_errors=1
phpSettings.date.timezone="Asia/GMT-8"
resources.frontController.controllerDirectory="../application/controllers"
resources.frontController.moduledirectory="../application/models"
resources.view.contentType = "text/html; charset=gbk"
也就不会出现'Invalid controller specified (error)'了
但我删掉这部分红色字体,又照样能够运行了,真不知是怎么回事,好像有什么东西被激活了似的。
xwt2000 2011-11-18
  • 打赏
  • 举报
回复
向楼主学习了,书签一下共同进步。
ohmygirl 2011-11-18
  • 打赏
  • 举报
回复
$this->_redirect('/login/success');

是大小写的问题么?

$this->_redirect('/Login/success');
LeoGeorge 2011-11-18
  • 打赏
  • 举报
回复
绝对不使用zendframework,又大有笨,用YII吧,php排名第一的框架,或者thinkphp,适合中国程序员
yinludrink 2011-11-18
  • 打赏
  • 举报
回复
另外谁回答我的这个问题:http://topic.csdn.net/u/20111118/11/74ebc998-db07-454f-8b12-4876e00430cf.html?40664
可以有100分的奖励,谢谢。
yinludrink 2011-11-18
  • 打赏
  • 举报
回复



<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//$frontController = Zend_Controller_Front::getInstance();
//$frontController->setBaseUrl('/mrzf/thanks');
$this->view->assign('title',$_SERVER["SERVER_NAME"].$this->_request->getBaseUrl());
//$this->view->assign('bod',$this->_request->getBaseUrl());
//$this->view->tit='';
//$this->view->bod='';
}
public function loginAction()
{
//$this->view->assign('title','zend framework框架测试成功!');
//$this->_forward("success","Login");
$this->_redirect("/mrzf/public/Login/success");
}

}
?>


<?php
class LoginController extends Zend_Controller_Action
{
public function logAction()
{
/*$this->view->assign('title','欢迎界面');
if($this->_request->isPost())
{
$username=$this->_request->getPost('username');
$password=$this->_request->getPost('password');
$auth=Zend_Auth::getInstance();
include '../application/class/AuthAdapter.php';
$authAdapter=new AuthAdapter(trim($username),trim($password));
$result=$auth->authenticate($authAdapter);
if($result->isValid())
{
$sessionNameSpace=new Zend_Session_Namespace('project');
$sessionNameSpace->username=$username;
$sessionNameSpace->password=$password;
$this->_redirect('/index/success');
}
else
{
echo '用户名和密码错误!';
}
}*/
//$this->view->assign('title','欢迎界面');
//echo 'hello world';
//$this->_redirect('http://'.$_SERVER["SERVER_NAME"].$this->_request->getBaseUrl().'/login/success');
$this->_forward("success","Login");
//$this->view->title=$_SERVER["SERVER_NAME"].$this->_request->getBaseUrl().'/login/success';
}
public function successAction()
{
$username=$this->_request->getPost('username');
$password=$this->_request->getPost('password');
$this->view->assign('title',$username.'<br/>'.$password);
echo 'hello zf';
//$this->_redirect("/index/login");
}
}
?>


C:\AppServ\www\mrzf\application\views\scripts\index\index.phtml
<html>
<head>
</head>
<body>
<div id="bodyDiv" align="center">
<h1><?php echo $this->title;?></h1>
<table bordercolor='green' border='1' cellspacing='0' cellpadding='4'>
<form action="<?=$this->url(array("controller"=>"index","action"=>"login"),null,true);?>" method="POST">
<tr>
<td>姓名</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="登录"/></td>
</tr>
</form>
</table>
</div>
</body>
</html>

index/login.phtml
<html>
<head>
</head>
<body>
<a href="#"><?php echo $this->title;?></a>
</body>
</html>

C:\AppServ\www\mrzf\application\views\scripts\login\success.phtml
<font size='4' color='red'><?php echo $this->title;?></font>

login\log.phtml
<font size='3' color='red'><?php echo $this->title;?></font>

C:\AppServ\www\mrzf\application\configs\application.ini
[project]
bootstrap.path="../application/Bootstrap.php"
bootstrap.class="Bootstrap"
phpSettings.display_errors=1
phpSettings.date.timezone="Asia/GMT-8"
resources.frontController.controllerDirectory="../application/controllers"
resources.view.contentType = "text/html; charset=gbk"

C:\AppServ\www\mrzf\application\Bootstrap.php
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function __initAutoload() {
$moduleAutoloader=new Zend_Application_Module_Autoloader(array('namespace'=>'','basePath'=>'../application'));
return $moduleAutoloader;
}
}
?>

C:\AppServ\www\mrzf\public\index.php
<?php
set_include_path('../library'.PATH_SEPARATOR.get_include_path());
require_once 'Zend/Application.php';
$application=new Zend_Application('project','../application/configs/application.ini');
$application->bootstrap()->run();

C:\AppServ\www\mrzf\public\.htaccess
RewriteEngine on
RewriteRule .* index.php

我的代码其实也很简单,但弄来弄去就是不行,为什么zendframework的_forward方法能用而_redirect方法却不能用?老是出现'Invalid controller specified (error)'

yinludrink 2011-11-18
  • 打赏
  • 举报
回复
我改成这样:
<?php
class IndexController extends Zend_Controller_Action
{
public function indexAction()
{
//$frontController = Zend_Controller_Front::getInstance();
//$frontController->setBaseUrl('/mrzf/thanks');
$this->view->assign('title',$_SERVER["SERVER_NAME"].$this->_request->getBaseUrl());
//$this->view->assign('bod',$this->_request->getBaseUrl());
//$this->view->tit='';
//$this->view->bod='';
}
public function loginAction()
{
//$this->view->assign('title','zend framework框架测试成功!');
//$this->_forward("success","Login");
$this->_redirect("/mrzf/public/Login/success");
}

}
但还是:'Invalid controller specified (error)'
coder 2011-11-18
  • 打赏
  • 举报
回复
或者
$this->_redirect(’/mrzf/public/login/success‘);
coder 2011-11-18
  • 打赏
  • 举报
回复
$this->_redirect($url);
$url是一个绝对路径例如http://www.baidu.com.
如果login是你的controller
succes是action
应该用
$this->_redirect(’http://localhost/mrzf/public/login/success‘);

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧