0

Namespacing in laravel is straightforward and aliasing the classes is fine, However I am having to alias even the most basic classes that are almost evident in every route like Input, Redirect etc. Without namespacing I dont have to use these they are preloaded and identified.

How do I use namespacing at the same time reserving the ability to just use Input & Redirect & Eloquent without having to alias/import in every class I want to use it?

Thanks

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Aiden Rigby
  • 569
  • 1
  • 5
  • 7

2 Answers2

1

That's just how namespaces work. The PHP Docs on namespaces give a pretty good explanation for the need to import/alias classes from different namespaces.

As far as your problem with Laravel facades goes, you can use them inside other namespaces by prepending the class name with \, as they are registred in the global namespace. So if you don't like to import them or use the fully qualified namespace, this would suffice:

$param = \Input::get('param');
return \Redirect::route('route');

You can register other global aliases in app/config/app.php under aliases.

Bogdan
  • 43,166
  • 12
  • 128
  • 129
1

This is because in Laravel 5 most classes like models, controllers, middleware, requests are places in some namespace. When you want to access class from other namespace you need to use fully qualified class (class with namespace) or import class first and then use shorter version. Otherwise you access classes from the same namespace.

I would like to emphasize - it's not Laravel issue, it's how namespaces work in PHP

For further explanation, you could read also How to use objects from other namespaces and how to import namespaces in PHP

Community
  • 1
  • 1
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291