简体   繁体   中英

CakePHP 1.3 Plugin Shortcut Route

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM