Kohana 中的 Request::$controller

发布于 2024-10-15 14:40:46 字数 415 浏览 5 评论 0原文

为什么我无法从视图请求使用的控制器的名称?

例如,someview.php 包含:

Kohana 显示错误:“ErrorException [ Fatal Error ]: Access to undeclared static property: Request::$controller”

为什么?怎么了?

我需要这样做:

; Example.com示例.com

Why I cannot request a used controller's name from the View?

For example, someview.php contains:

<?php
echo Request::$controller;
?>

Kohana shows the error: “ErrorException [ Fatal Error ]: Access to undeclared static property: Request::$controller”

Why? What's wrong?

It is needed for me for doing this:

<?php if (Request::$controller != 'index') { ?>
<a href="/">Example.com</a>
<?php } else { ?>
Example.com
<?php } ?>

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

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

发布评论

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

评论(5

凹づ凸ル 2024-10-22 14:40:46

相反,在控制器上执行此操作:

View::bind_global('controller', $this->request->param('controller'));

然后您可以在任何视图上访问 $controller

Do this instead, on the controller :

View::bind_global('controller', $this->request->param('controller'));

Then you can access $controller on any view.

一绘本一梦想 2024-10-22 14:40:46

请求应该通过它的静态方法访问,不需要定义额外的静态属性 ||全局视图变量来获取它。

Request::instance() 将返回主请求实例(“母实例”)。
Request::current() 将返回当前活动请求的实例,与您可以在 Controller 中使用 $this->request 访问的内容相同。

<? if (Request::current()->controller !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>

Request should be accessed by it's static methods, there's no need to define additional static properties || global view vars to get it.

Request::instance() will return the the main request instance ('mother instance').
Request::current() will return the instance of the currently active request, the same thing you can access with $this->request in Controller.

<? if (Request::current()->controller !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>
靑春怀旧 2024-10-22 14:40:46

我会按照尤达的建议去做,但是我可能也会将该逻辑放入控制器中。

我猜你想要一个返回主页的链接?

$link = (Request::$controller != 'index') ? '<a href="/">Example.com</a>' : 'Home';
$this->template->set_global('homeLink', $link);

也不要忘记,您可以使用 Route::get() 或其朋友之一从您的路线构建链接。

I would do as yoda suggested, however I would probably put that logic in the controller as well.

I assume you want a link back to home?

$link = (Request::$controller != 'index') ? '<a href="/">Example.com</a>' : 'Home';
$this->template->set_global('homeLink', $link);

Don't forget too you can build links from your routes by using Route::get() or one of its friends.

赠我空喜 2024-10-22 14:40:46

在 Kohana 3.1 中

<? if (Request::current()->controller !== 'index') : ?>

给出“ErrorException [通知]:未定义的属性:Request::$controller”。然后我只是使用 Request::current()->controller() 来查看可接受/最佳实践/最佳性能?

<? if (Request::current()->controller() !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>

In Kohana 3.1

<? if (Request::current()->controller !== 'index') : ?>

give the "ErrorException [ Notice ]: Undefined property: Request::$controller". then i simply using Request::current()->controller() in view acceptable/ best practice/ optimal performance?

<? if (Request::current()->controller() !== 'index') : ?> 

    <a href="<?= URL::site() ?>">Example.com</a> 

<? else : ?> 

    Example.com 

<?  endif; ?>
你的他你的她 2024-10-22 14:40:46

Kohana 3.2:在控制器中,粘贴这个(我发现你不能使用bind_global真的很愚蠢)

View::set_global('controller', $this->request->current()->controller());

然后在视图中,你可以使用:

echo ( $controller );

Kohana 3.2: In the controller, paste this (I find it really really stupid that you can't use bind_global)

View::set_global('controller', $this->request->current()->controller());

Then in the view, you can use:

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