【问题标题】:Cakephp Passed argumentsCakephp 传递的参数
【发布时间】:2013-02-05 07:22:19
【问题描述】:

您好,我的代码在 CakePHP 框架中无法运行,并且显示错误消息。

网址:

http://domainname.com/About/param1/param2

代码:

class AboutController extends AppController {

    public $name = 'About';
    public $helpers = array('Html');

    public function index($arg1, $arg2)
    {
        print_r($this->request->params['pass']);
        $this->set('title_for_layout','Sarasavi Bookshop - About Sarasavi Bookshop');
        $this->set('nameforfiles','about');
    }
}

错误信息:

Missing Method in AboutController    
Error: The action param1 is not defined in controller AboutController
Error: Create AboutController::param1() in file: app\Controller\AboutController.php.

<?php
class AboutController extends AppController {


    public function param1() {

    }

}

Notice: If you want to customize this error message, create app\View\Errors\missing_action.ctp

创建函数param1后,我可以得到param2,但是我需要同时得到param1param2,在index函数中没有创建另一个动作。

请帮帮我, 谢谢

【问题讨论】:

    标签: php cakephp routing url-routing


    【解决方案1】:

    如果您转到http://domainname.com/About/index/param1/param2,您的原始代码将可以工作

    如果您不希望 url 中出现 index,我假设您不希望,那么您需要定义一个路由。

    将此添加到您的路线中:

    Router::connect(
        '/About/*',
        array('controller' => 'About', 'action' => 'index')
    );
    

    自动路由未指定操作但具有参数的请求以转到您的 index 操作。您需要为任何新的 About 操作添加路由,以阻止它们的请求默认进入索引。

    【讨论】:

      【解决方案2】:

      您可能访问此 URL 时未指定操作名称(索引),如下所示:

      /about/param1/param2

      这将不起作用,因为 Cake 期望在控制器名称之后看到动作名称。在这种情况下,param1 被视为操作名称。 您可以通过 URL 访问您的操作

      /about/index/param1/param2

      要克服这种情况,请为您的路由器制定新规则:

      Router::connect(
          '/about/:param1/:param2',
          array('controller' => 'about', 'action' => 'index'),
          array('param1', 'param2')
      );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 1970-01-01
        • 1970-01-01
        • 2013-03-11
        相关资源
        最近更新 更多