[英]Laravel 4 class not found and namespaces
In a Laravel 4 site I have installed a tag cloud library. 在Laravel 4站点中,我安装了标签云库。 In this library I wish to edit the return line of a function (I have simplified it here):
在这个库中,我希望编辑一个函数的返回行(这里我已经简化了):
return "<span>
{$Variable}
</span>";
and make the $variable a link: 并使$ variable为链接:
return "<span>
<a href='".Request::url()."/myroute/'>
{$variable}
</a>
</span>";
When I try to run the page I get the error: 当我尝试运行页面时,出现错误:
Class 'Arg\Tagcloud\Request' not found
I thought that it might mean that the Request class is not visible within the tagcloud class and it has to do with namespaces. 我认为这可能意味着Request类在tagcloud类中不可见,并且与名称空间有关。
On top of the tagcloud class file there are these lines: 在tagcloud类文件的顶部,有以下几行:
<?php namespace Arg\Tagcloud;
use Illuminate\Support\Facades\Facade;
I have found this page (a list of laravel namespaces) http://laravel.com/api/index.html 我已经找到此页面(laravel名称空间列表) http://laravel.com/api/index.html
and I have used 我已经用过
use Illuminate\Routing;
or 要么
use Illuminate;
I added them just below the above first two 'use' statements but I get the same error (class not found). 我在上面的前两个“ use”语句的下面添加了它们,但是得到了相同的错误(找不到类)。 Is it a matter of finding the right namespace or something else?
找到正确的名称空间还是其他问题? Can anyone help?
有人可以帮忙吗? Thanks.
谢谢。
EDIT: I found this way of doing what I wanted without using Request::url(): 编辑:我发现这种无需使用Request :: url()的方式就可以做到:
return "<span>
<a href='/mysitename/public/myroute/{$variable}'>
{$variable}
</a>
</span>";
Whenever you put your class in a namespace, any other class that you reference in that class assumes that it is also in that same namespace unless you tell it otherwise. 每当您将类放在命名空间中时,在该类中引用的任何其他类都将假定该类也位于同一命名空间中,除非另有说明。
So, since you set your namespace to Arg\\Tagcloud
when you directly reference Request::url()
, PHP thinks that you're telling it to use the Request
class inside the Arg\\Tagcloud
namespace. 因此,由于在直接引用
Request::url()
时将名称空间设置为Arg\\Tagcloud
,因此PHP认为您是在告诉它使用Arg\\Tagcloud
命名空间内的Request
类。
There's two solutions to this: 有两种解决方案:
Add a \\
in front of Request
to tell PHP to use the global namespace, not the Arg\\Tagcloud
namespace. 在
Request
前面添加一个\\
,以告诉PHP使用全局名称空间,而不是Arg\\Tagcloud
名称空间。
eg, <a href='".\\Request::url()."/myroute/'>
例如,
<a href='".\\Request::url()."/myroute/'>
use
the class. use
该类。
Just add use Request
to the top of your class (under your namespace), this will let PHP know that you want to use this class, but it doesn't belong in the current namespace. 只需将
use Request
添加到类的顶部(在名称空间下),这将使PHP知道您要使用此类,但它不属于当前名称空间。
eg, 例如,
<?php namespace Arg\Tagcloud;
use Request;
When I try to run the page I get the error:
当我尝试运行页面时,出现错误:
Class 'Arg\\Tagcloud\\Request' not found
It's because you are using the Request
class from Arg\\Tagcloud
namespace and when you use Request::someMethod()
framework things that Request
class is under same namespace which is: 这是因为您使用的是
Arg\\Tagcloud
命名空间中的Request
类,并且当您使用Request::someMethod()
框架时, Request
类位于同一命名空间下:
Arg\Tagcloud\Request
But, actually, Request
class is under, Illuminate\\Http
namespace and that's why the error is being occurred. 但是,实际上,
Request
类位于Illuminate\\Http
命名空间下,这就是发生错误的原因。 To overcome this problem you may use something like this: 为了克服这个问题,您可以使用以下方法:
\Request::someMethod();
Or you may use following at the top of your class: 或者您可以在班级顶部使用以下代码:
use \Request;
You don't need to use use Illuminate\\Request;
您无需使用
use Illuminate\\Request;
because Request
facade class is available in the global namespace (as an alias) which will resolve the Illuminate\\Http\\Request
class. 因为
Request
外观类在全局名称空间中可用(作为别名),它将解析Illuminate\\Http\\Request
类。 So, you can directly use Request::someMethod()
in your code. 因此,您可以在代码中直接使用
Request::someMethod()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.