Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检测当前页面是否是带有CakePhp的主页?_Php_Cakephp_Cakephp 2.0 - Fatal编程技术网

如何检测当前页面是否是带有CakePhp的主页?

如何检测当前页面是否是带有CakePhp的主页?,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,如何使用CakePhp检测用户是否在我的网站主页上 我可以使用$this->webroot 目标是仅当当前页面是主页时才执行某些操作。您可以使用$this->request->query['page']确定您的位置 if ( $this->request->query['page'] == '/' ){ //do something } 编辑: 使用echo debug选中$this->request对象($this->request),它包含许多可以使用的信息。以下是您获得

如何使用CakePhp检测用户是否在我的网站主页上

我可以使用
$this->webroot


目标是仅当当前页面是主页时才执行某些操作。

您可以使用$this->request->query['page']确定您的位置

if ( $this->request->query['page'] == '/' ){
   //do something
}
编辑:

使用echo debug选中$this->request对象($this->request),它包含许多可以使用的信息。以下是您获得的示例:

object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'pages',
        'action' => 'display',
        'named' => array(),
        'pass' => array(
            (int) 0 => 'home'
        )
    )
    data => array()
    query => array()
    url => false
    base => ''
    webroot => '/'
    here => '/'
}

假设您要从AppController执行某些操作,最好查看当前控制器/操作对是否是您定义为“主页”的控制器/操作对(因为Cake可以将用户路由到“/”路由的任何位置,并且您可能仍然希望在使用完整的
/controller/action
URI直接调用操作时触发逻辑,而不仅仅是在
/
上)。在AppController中,只需添加一个检查:

if ($this->name == 'Foo' && $this->action == 'bar') {
    // Do your stuff here, like
    echo 'Welcome home!';
}

这样,每当从
FooController
请求
bar
操作时,它就会触发。显然,您也可以将此逻辑放在特定的控制器操作本身中(这可能更有意义,因为它的开销较小)。

您可以尝试以下方法:

if ($this->request->here == '/') {
       // some code
}
此外,阅读以下部分也很好:

您可以使用CakeRequest来反省有关 除了探测器,你还可以找到其他信息 从各种属性和方法

$this->request->webroot contains the webroot directory.
$this->request->base contains the base path.
$this->request->here contains the full address to the current request
$this->request->query contains the query string parameters.

您可以通过检查以下参数来正确获取它:

if($this->params['controller']='homes'&&&$this->params['action']='index')


通过这种方式,您可以在视图端检查cakephp的任何页面

您可以通过将当前页面与webroot或base进行比较来找到它

if ($this->here == $this->webroot){ // this is home page }


如果您的主页是cakePHP约定中提到的home.ctp。在PagesController中,您可以将显示函数更改为:

(添加的代码从注释开始/*自定义代码开始*/)


我实现这一点的方法是使用
$this->params
;,您将看到该变量的内容。它将返回一个数组。您将看到您在主页上时与不在主页上时的差异。您将必须使用
$this->params
中的一个键来使用
if
语句进行计算。这就是我实现它的方法。也许您可以找到它这个方法也很有用。

我不知道cakephp,但是
\uuuuu file\uuuuu
将输出文件的路径,您可以检查路径是否与主页的路径相同..我试过了,但不起作用。我想$this->request->query['page']意思是你有一个查询字符串。但是我的url更像www.example.com而不是www.example.com?page=…请看我的编辑,你可能会在$this->request object中找到你需要的东西,这正是我要找的!谢谢!谢谢!我发现了一个新变量)这在子文件夹下不起作用,例如
http://localhost/cakephp/
。在这种情况下,您可以尝试这不是一个标准的解决方案,如果您有多种语言(/fr),该怎么办。
if ($this->here == $this->base.'/'){ // this is home page }
public function display()
{
    $path = func_get_args();

    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    /* Custom code start*/
    if("home"==$page){
        // your code here
    }
    /* Custom code end*/
    $this->set(compact('page', 'subpage'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingTemplateException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}