1

我在网上搜索了很多,但找不到任何具体的解决方案。
在 CakePHP 1.3 中,与 1.2 不同,如果您在插件中有一个控制器,并且两者具有相同的名称,您可以通过“<plugin>/<action>”访问,它会调用“默认”控制器。但是在 1.3 中,根据这个:

http://cakeqs.org/eng/questions/view/setting_up_magic_routes_for_plugins_in_cakephp_1_3

它已被删除,并且只能通过这种方式访问​​默认插件控制器中的“索引”操作。

我考虑在我的 routes.php 文件中添加额外的代码,并遍历我的应用程序中的所有插件,为以插件命名的控制器中的每个操作创建这样的路由,但这似乎不是正确的做法...

在 1.3 中进行这项工作的任何其他建议?或者至少是这个特定更改的一些非常具体的代码文档?我已经阅读了 1.3.0-RC4 公告中的一些内容,但还不够清楚..

谢谢

4

1 回答 1

0

假设有一个名为“test”的插件,您可以在 app/plugins/test/controller/test_controller.php 中执行以下操作:

<?php
class TestController
    extends AppController
{
    public function index()
    {
        // Is there any additional args passed to us?
        if(count($this->passedArgs) > 0)
        {
            // Is this a request for one of our actions?
            $actualAction = $this->passedArgs[0];
            if(is_callable(array($this, $actualAction)))
            {
                // Yup. Do it.
                return call_user_func_array(array($this, $actualAction), array_slice($this->passedArgs, 1));
            }
        }

        // Default functionality here.
        die("Index of plugin requested.");
    }

    public function another($param1, $param2)
    {
        die("{$param1}, {$param2}");
    } 
}

您还必须将以下内容添加到 app/config/routes.php:

Router::connect("/test/*", array("plugin" => "test", "controller" => "test"));

完成此操作后,对 /test/another/one/two 的请求将在浏览器中正确呈现“一,二”,对 /test 的请求将显示“请求的插件索引”。

我认为这不是一个糟糕的方法,插件消费者方面的小题大做,插件代码中只有一点点绒毛。

于 2010-09-20T04:24:24.123 回答