Resource Routes - RESTful Routes in Laravel
Laravel, PHP
Laravel allows you to quickly and easily set up controllers and routes so that you can interact with data in a RESTful way..
Create a Model and Database Table
Create a Model in the usual way (the -m
flag creates a migration for the model, allowing you to set up a new database table):
Amend the migration as necessary and run php artisan migrate
to set up the new table.
Create a Resourceful Controller
Create a controller using Artisan using the --resource
option:
This creates a new controller under app/Http/Controllers
, with stubbed out methods to allow interaction with the database table for this resource:
index()
: Display a listing view for the resourcecreate()
: Show the form for creating a new resourcestore()
: Store a newly created resource in storageshow()
: Display the specified resourceedit()
: Update the specified resource in storagedestroy()
: Remove the specified resource from storage
Ruby on Rails has avery similar command: rails g scaffold_controller Article
- which stubs out a similar controller.
Create Route for the Resource
In your routes/web.php
file (Laravel 5.3), create a new route using the resource()
method:
If you check the registered routes now using php artisan route:list
, you’ll see the new (named) routes automatically set up:
The resource has been set up so that the correct controller methods are triggered when you access a specified route with a given HTTP verb.
comments powered by Disqus