PHPEye开源社区 » Zend Framework 使用讨论 » ZF路由分发时控制器与动作奇怪问题,求助!
《Programming PHP》第二版上市
2008-1-23 13:07 kylingood
ZF路由分发时控制器与动作奇怪问题,求助!

各位高人..本人在开始用ZF做一个项目...我分二个模块...第一个是前台(default).另一个是后台(admin).
    我在前台页面有一个链接到后台的动作...比如说..我在aaa.php这个页面里有这么一个动作..当我点链接的时候.他可以进入到后台(admin)的(bbb.php)页面....(也就是证明了路由分发到了这个控制器并产生了index这个动作).可是当我点击bbb.php页面上的链接(这里的链接是admin这个模块里,存在的控制器上的都存在的动作)时候.就会显示出aaa.php 的页面....不知道是怎么一回事..下面贴我的index.php文件出来..哎...好晕...希望有高手帮小弟一把...

下面是index.php文件..不知道是不是这里的问题:

[php]
<?php
error_reporting(E_ALL|E_STRICT);
//date_default_timezone_get('Asia/Shanghai');
date_default_timezone_set('Asia/Shanghai');
define('ROOT','../shop/');//定义网站根目录
define('DIR_STYLE',ROOT.'public/styles/');//定义样式目录
set_include_path( '.'. PATH_SEPARATOR .
      './library/'. PATH_SEPARATOR .
      './application/modules/default/models/'. PATH_SEPARATOR .
      get_include_path());
require_once 'Smarty/Smarty.class.php';
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload(); //自动加载类文件
$registry = Zend_Registry::getInstance();  //实例化注册对象
/*网站主文件SMARTY配置文件*/
$smarty=new Smarty();
$smarty->left_delimiter="<{";
$smarty->right_delimiter="}>";
$smarty->compile_dir="./application/modules/default/views/templates_c";
$smarty->template_dir="./application/modules/default/views/templates";
$registry['smarty']=$smarty;

/*后台管理员SMARTY配置文件*/
$smart=new Smarty();
$smart->left_delimiter="<{";
$smart->right_delimiter="}>";
$smart->compile_dir="./application/modules/admin/views/templates_c";
$smart->template_dir="./application/modules/admin/views/templates";
$registry['smart']=$smart;

//读取配置文件
require_once 'Zend/Config/Ini.php';
$config=new Zend_Config_Ini('./application/config/config.ini','general');
$registry['config']=$config;

//连接到数据库
$db=Zend_Db::factory($config->db->type,$config->db->config->toArray());
Zend_Db_Table::setDefaultAdapter($db);
$registry['db']=$db;

$auth=Zend_Auth::getInstance();//实例化用户认证类
$acl=new ShopAcl();//实例化用户权限类

//首先通过getInstance()获取前端控制器实例,然后通过setControllerDirectory() 注册传入的路径,最后分发.
$frontController = Zend_Controller_Front::getInstance();
//取到控制器目录的当前位置
$frontController->addModuleDirectory('./application/modules')
     ->setBaseUrl('/shop')//设置基地址
     ->setParam('useDefaultControllerAlways',true)//缺省或者错误控制器时使用默认控制器
     ->setParam('noViewRenderer',true)//禁止自动动视图渲染
     ->setParam('noErrorHandler',true)//禁止自动错误处理
     ->throwExceptions(true)
     ->registerPlugin(new ShopAuth($auth,$acl))
     ->dispatch();

[/php]

[[i] 本帖最后由 kylingood 于 2008-1-23 13:11 编辑 [/i]]

2008-1-24 02:40 jasonqi
能否多提供点资料?

2008-1-24 08:36 kylingood
[quote]原帖由 [i]jasonqi[/i] 于 2008-1-24 02:40 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=1256&ptid=347][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
能否多提供点资料? [/quote]
可以.下面是我的项目目录:

--------shop
             ----application
                    -----config
                    -----modules
                              -----default
                                     ------controllers
                                             ---IndexController.php
                                     ------models
                                     ------views

                              -----admin
                                     ------controllers
                                             ---IndexController.php
                                             ---ProductController.php
                                     ------models
                                     ------views

             ----public
             ----library
             ----index.php
我的问题在于我前台模块是default...网站先通过default模块里的控制器IndexController.php访问aaa.php页面.在aaa.php页面中有一个链接动作到模块admin中..也就是通过admin模块中的IndexController.php来显示出bbb.php 页面.这样可以访问到bbb.php页面.但是在bbb.php页面里,我要用ProductController.php控制器中的productshow动作来显示商品列表时..我点击这个链接的时候就会返回到aaa.php页面...奇怪的就是这样...
下面是default 模块中的 IndexController.php:
[php]
<?php
class IndexController extends Zend_Controller_Action
{
                public $smarty;
                public function     init(){
                        $this->smarty=Zend_Registry::get('smarty');
                        $this->smarty->assign('pathcss',$this->_request->getBasePath());
                        $this->smarty->assign('adminpath',$this->_request->getBaseUrl());
                }
                public function     indexAction(){
                        $this->smarty->display('index.phtml');(这里就是上面说的aaa.php页面)
                }
                               
}
?>
[/php]
下面二个文件是admin模块中的 IndexController.php和ProductController.php:
[php]
<?php

        class Admin_IndexController extends Zend_Controller_Action{
               
                public $smart;

                public function        init(){
                        //$this->registry = Zend_Registry::getInstance();
                        $this->smart=Zend_Registry::get('smart');
                        $this->smart->assign('admincss',$this->_request->getBasePath());
                        $this->smart->assign('adminpath',$this->_request->getBaseUrl());
                }
               
                public function indexAction(){
               
                        $this->smart->display('top.phtml');
                        $this->smart->display('main.phtml');(这里是上面说的bbb.php)
                }
               
                public function        __call($method,        $args)
                {
                        if ('Action' ==        substr($method,        -6)) {
                                $controller        = $this->getRequest()->getControllerName();
                                $url = '/' . $controller . '/index';
                                return $this->_redirect($url);
                        }

                        throw new Exception('Invalid method');
                }       
        }
[/php]
[php]
<?php

        class Admin_ProductController extends Zend_Controller_Action{
               
                public $smart;

                public function        init(){
                        $this->smart=Zend_Registry::get('smart');
                        $this->smart->assign('admincss',$this->_request->getBasePath());
                        $this->smart->assign('adminpath',$this->_request->getBaseUrl());
                }

                public function productshowAction(){
                        $this->smart->display('top.phtml');
                        $this->smart->display('productshow.phtml');
                }

[/php]

2008-1-24 15:35 jasonqi
[php]    /**
     * Specify a directory as containing modules
     *
     * Iterates through the directory, adding any subdirectories as modules;
     * the subdirectory within each module named after {@link $_moduleControllerDirectoryName}
     * will be used as the controller directory path.
     *
     * @param  string $path
     * @return Zend_Controller_Front
     */
    public function addModuleDirectory($path)
    {
        $dir = new DirectoryIterator($path);
        foreach ($dir as $file) {
            if ($file->isDot() || !$file->isDir()) {
                continue;
            }

            $module    = $file->getFilename();

            // Don't use SCCS directories as modules
            if (preg_match('/^[^a-z]/i', $module) || ('CVS' == $module)) {
                continue;
            }

            $moduleDir = $file->getPathname() . DIRECTORY_SEPARATOR . $this->getModuleControllerDirectoryName();
            $this->addControllerDirectory($moduleDir, $module);
        }

        return $this;
    }[/php]

上面是addModuleDirectory函数的源代码,看看它是不是把你赋给 $path 变量的值 ('./application/modules')处理对了。

我猜问题在这里,但没有验证,希望有经验的同学看一哈。

2008-1-24 17:16 kylingood
这一层是处理对了..可是到了admin模块里面就会回到原来的页面.....
不知道我哪里写的有问题....:L :L

2008-1-25 00:00 jasonqi
[quote]原帖由 [i]kylingood[/i] 于 2008-1-24 17:16 发表 [url=http://www.phpeye.com/bbs/redirect.php?goto=findpost&pid=1260&ptid=347][img]http://www.phpeye.com/bbs/images/common/back.gif[/img][/url]
这一层是处理对了..可是到了admin模块里面就会回到原来的页面.....
不知道我哪里写的有问题....:L :L [/quote]

你是不是说已经执行到这里了?(我的意思是你通过无论什么调试器,你已经看到程序已经跑到这里了)

[php] public function indexAction(){
        
            $this->smart->display('top.phtml');
            $this->smart->display('main.phtml');(这里是上面说的bbb.php)
        }[/php]

如果这样,那和ZF的控制器就没有关系了,看看你的smarty是不是有问题。

2008-1-25 09:17 kylingood
是的...可以执行这个页面....控制器可以分到这个页面来...可能是smarty问题...但是不知道怎样改才好....我的smarty配置在上面index.php上,,因为要分开模板页..所以配置了二个smarty,也不知道这样做对不对......

2008-1-27 17:43 玉面修罗
怀疑你的URL路由信息。。
把你生成的链接地址都发上来。
严重怀疑URL路由信息错误导致呼叫了不存在的action导致了__call调用,然后显示了首页信息。

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.