CakePHP 1.3 插件快捷方式

发布于 2024-09-24 13:26:11 字数 624 浏览 2 评论 0原文

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

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

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

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

还有其他建议可以在 1.3 中实现此功能吗?或者至少有一些关于这个特定更改的非常具体的代码文档?我已经读过 1.3.0-RC4 公告中的一些内容,但还不够清楚..

谢谢

I searched a lot around the web but I couldn't find any specific sollution to this.
In CakePHP 1.3, different from 1.2, if you had a controller inside a plugin, and both had the same name, you could access through "<plugin>/<action>", and it would call the 'default' controller. But in 1.3, according to this:

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

It was removed, and only the 'index' action in the default plugin controller can be accessed this way.

I thought about adding extra code in my routes.php file, and loop through all the plugins in my app, making such routes for every action in the controllers named after the plugin, but it doesn't seem like it's the right thing to do...

any other suggestions to make this work in 1.3? or at least some very specific code documentation of this particular change? I've already read something in the 1.3.0-RC4 annoucement, but it was not clear enough..

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

享受孤独 2024-10-01 13:26:11

假设有一个名为“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 的请求将显示“请求的插件索引”。

我认为这不是一个坏方法,插件消费者方面的麻烦最小,插件代码中只有一点点绒毛。

Assuming a plugin named "test", you could do something like this in 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}");
    } 
}

You'll also have to add the following to app/config/routes.php:

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

With this done, a request to /test/another/one/two will correctly render "one, two" in the browser, and a request to /test will display "Index of plugin requested."

I think this isn't a bad way to go, minimal fuss on the plugin consumer side, only a little bit of fluff in the plugin code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文