{info} If you are not familiar with its concept. Check out the full Laravel Controller documentation to get started.
Create Laravel Controller file and keep it in your app’s /src/Http/Controller
folder.
/packages/vendor-name/project-name/
├── src
│ ├── Http
│ │ ├── Controllers
│ │ │ └── Admin
│ │ │ └── AdminDashboardController.php
│ │ │ └── User
│ │ │ └── UserDashboardController.php
│ │ └── Requests
If you set up App route using Route::group
suggested on App Routing, you will need to separate controller accordingly.
namespace VendorName\ProjectName\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class AdminDashboardController extends Controller
{
public function index()
{
return view('admin.dashboard');
}
}
namespace VendorName\ProjectName\Http\Controllers\User;
use App\Http\Controllers\Controller;
class UserDashboardController extends Controller
{
public function index()
{
return view('user.dashboard');
}
}
Create Laravel Request file and keep it in your app’s /src/Http/Requests folder.
/packages/vendor-name/project-name/
├── src
│ ├── Http
│ │ ├── Controllers
│ │ └── Requests
│ │ │ └── Admin
│ │ │ └── ExampleRequest.php
│ │ │ └── User
Here is an example of ExampleRequest
:
namespace VendorName\ProjectName\Http\Requests\Admin;
use Illuminate\Foundation\Http\FormRequest;
class ExampleRequest extends FormRequest
{
public function authorize()
{
return $this->user()->isAdmin();
}
public function rules()
{
return [
'attribute-name' => 'required',
];
}
public function messages()
{
return [];
}
}