【问题标题】:Cakephp - get controller name and action from url when working in a different controllerCakephp - 在不同的控制器中工作时从 url 获取控制器名称和操作
【发布时间】:2013-12-31 15:46:21
【问题描述】:

我有一个控制器“用户”和动作“索引”,它是带有“$this->requestAction”的视图文件,我正在调用不同的控制器和动作。像这样:

index.ctp

//My html code
<?php echo $this->requestAction('anotherController/anotherAction'); ?>
//My html code

现在,问题出在“anotherController”和“anotherAction”中,我想用“$this->params”数组获取请求的控制器“用户”和操作“索引”,但它给了我“另一个控制器”作为当前控制器和“另一个动作”作为当前动作。喜欢

anotherController.php

public function anotherAction(){
  pr($this->params);
  //It gives me
  //['controller'] = anotherController
  //['action'] = anotherAction
  //I want to get the requested controller & action of url here
  //which should be 'Users' & 'index'
}

$_SERVER['REQUESTED_URI'] 给了我 url,但是有没有更好的方法来从 URL 中找出 Controller 和 Action 是什么?

【问题讨论】:

  • 说明为什么要使用请求操作会很有用——通常在控制器中使用请求操作表明应用程序设计不佳。 (例如,在应该在模型中的控制器操作中调用代码)。
  • 是的,确实如此,我知道这是一种糟糕的编码风格,但我不是很擅长蛋糕。我试图根据控制器和动作和参数在我的布局中生成 ,这就是为什么当我不知道将调用哪个控制器或动作时,我在布局文件中调用 requestAction,如果你有更好的想法,请给我建议。谢谢
  • 在没有任何代码/细节的情况下,我的建议是:不要那样做 =)。

标签: cakephp controller


【解决方案1】:

您可以将其作为参数传递

$this->requestAction(
    array('controller' => 'anotherController', 'action' => 'anotherAction'),
    array('pass' => array('curentController','currentAction'))
);

然后你可以得到参数为

$this->request->params['pass']

requestAction 的语法:requestAction(string $url, array $options)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    相关资源
    最近更新 更多