I am trying to store my models in a custom namespace and directory structure as shown here:
I have:
namespace Modules\Core;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
But I am getting:
FatalErrorException in TestController.php line 8:
Class 'Modules\Core\User' not found
Which is wrong anyway, so I thought it must be 'Modules\\Core\\Models\\User'. I tried this and I still got the same error (just with a different class name).
My model:
namespace Modules\Core;
use Illuminate\Database\Eloquent\Model as Eloquent;
class User Extends Eloquent {
protected $table = 'users';
}
How can I get access to this model in the TestController?
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => 'TestController@index']);
});
If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\\Core\\Controllers;
And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\\Core\\Models;
Then, in the controller, import it before using it:
<?php namespace Modules\Core\Controllers;
use Modules\Core\Models\User;
use App\Http\Controllers\Controller;
class TestController extends Controller {
public function index(){
$user = new User;
return view('core::test');
}
}
I had the same problem as above. In my case I had the following namespace:
namespace Modules\XMLApi;
I got the same error as above. When I changed the namespace to the following:
namespace Modules\XmlApi;
Then run the following command: composer dump-autoload
Then it worked.
You should edit your routes.php
file:
Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
Route::get('/test', ['uses' => '\Modules\Core\TestController@index']);
});
to use full together with namespace
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.