Controllers manage the logic and are stored in the app/Http/Controllers folder.
The following command can be used to generate a simple (empty) controller:
php artisan make:controller ExampleController
Your newly created ExampleController will look as follows:
< ?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ExampleController extends Controller
{
// implement functions here
}
If you want to generate all CRUD methods at once, use:
php artisan make:controller ExampleController --resource
This command will create a Resource Controller with the methods index, create, store,
show, edit, update and destroy.
class ExampleController extends Controller
{
public function index() { ... }
public function create() { ... }
public function store(Request $request) { ... }
public function show(string $id) { ... }
public function edit(string $id) { ... }
public function update(Request $request, string $id) { ... }
public function destroy(string $id) { ... }
}
Route Overview for ExampleController:
| Verb | URI | Action | Method Name |
|---|---|---|---|
| GET | /posts |
index | posts.index |
| GET | /posts/create |
create | posts.create |
| POST | /posts |
store | posts.store |
| GET | /posts/{post} |
show | posts.show |
| GET | /posts/{post}/edit |
edit | posts.edit |
| PUT/PATCH | /posts/{post} |
update | posts.update |
| DELETE | /posts/{post} |
destroy | posts.destroy |