I am new to laravel. I am trying to organise my controller by putting it inside a folder, but it doesn't seem to work.
My folder structure is like this:
/app
/Http
/Controllers
/Admin
ShowDashboard.php
My ShowDashboard.php file is like this:
<?php namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class ShowDashboard extends Controller {
/**
* Show the profile for the given user.
*
* @param int $id
* @return Response
*/
public function init()
{
return 'Hi there!';
}
}
My route is like this
Route::get('/admin', 'Admin\ShowDashboard@init');
When I tred to access http://localhost:8000/admin I get the following error:
Class App\Http\Controllers\Admin\ShowDashboard does not exist
My autolaoder section:
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
}
Am I missing something?
Create a new controller in subfolder, for example:
app/Http/Controllers/User/UserController.php
In this controller, at the end of
namespace
must include folder nameLike this:
namespace App\Http\Controllers\User;
The important thing is under namespace must write
use App\Http\Controllers\Controller;
finally in routes.php
Route::get ( '/user', 'User\UserController@login' );
UserController.php contents:
routes.php contents: