Reputation: 97
In a Laravel 4 site I have installed a tag cloud library. 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:
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.
On top of the tagcloud class file there are these lines:
<?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
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). 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():
return "<span>
<a href='/mysitename/public/myroute/{$variable}'>
{$variable}
</a>
</span>";
Upvotes: 2
Views: 2843
Reputation: 146191
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
But, actually, Request
class is under, Illuminate\Http
namespace and that's why the error is being occurred. 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;
because Request
facade class is available in the global namespace (as an alias) which will resolve the Illuminate\Http\Request
class. So, you can directly use Request::someMethod()
in your code.
Upvotes: 0
Reputation: 8577
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.
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.
e.g., <a href='".\Request::url()."/myroute/'>
use
the class.
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.
e.g.,
<?php namespace Arg\Tagcloud;
use Request;
Upvotes: 3