Reputation: 833
I have decided to ditch the model folder and move everything to a repo structure: app/Acme/Foo
Everything is correctly namespaced, but when I try to access my Bar
class inside app/Acme/Foo/Bar.php
I can only instantiate it as new Acme\Foo\Bar();
. Otherwise laravel looks for it inside the models folder. The only workaround is namespace aliasing on top of the file which implements the Bar
class. Any workaround? From what I understand laravel 5 handles this automagically, but switching is not a preferred option
Upvotes: 2
Views: 2034
Reputation: 166106
I can only instantiate it as new Acme\Foo\Bar(); ... The only workaround is namespace aliasing on top of the file which implements the Bar class. Any workaround?
There's (mostly, see below) no work around, this is how modern PHP works. Your class's full name is actually Acme\Foo\Bar
, so that's what you need to use to reference it. If you want to reference it by a short-name (Bar
), then your use the use
statement at the top of your file after the namespace declaration. This is how things work now.
Laravel does have a solution for this. If you look in app/config/app.php
, you'll see the following section
#File: app/config/app.php
...
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
'Artisan' => 'Illuminate\Support\Facades\Artisan',
'Auth' => 'Illuminate\Support\Facades\Auth',
'Blade' => 'Illuminate\Support\Facades\Blade',
'Cache' => 'Illuminate\Support\Facades\Cache',
...
'Eloquent' => 'Illuminate\Database\Eloquent\Model',
The array lets you setup short aliases for fully namespaced classes. Laravel uses it mostly for Facades, but it's also what allows you to do class MyModel extends Eloquent
, where Eloquent
is really Illuminate\Database\Eloquent\Model
. You could add your own alias to this array
'Bar' => 'Acme\Foo\Bar'
And then refer to your class globally with Bar
. However, you'll probably be better off getting used to the idea of namesapced classes, as that's the direction most PHP frameworks are headed.
Also, if you're curious, the aliases in the aliases
array above are implemented via PHP's class_alias
function.
Upvotes: 4