Laravel Project
In this Laravel project tutorial we will learn to create Routes.
Route files are present inside the routes directory.
For this project we will be working with the routes/web.php file to create routes for the web requests.
routes/web.php
Routes are used to map URLs to controllers or anonymous functions.
web.php
If we open the web.php route file we will get to see the following code.
Route::get('/', function () { return view('welcome'); });
The above code is instructing Laravel that if you receive a GET request for the document root URL / then return the welcome view.
Requests can be GET, POST, PUT, PATCH, DELETE etc.
GET
POST
PUT
PATCH
DELETE
Views are saved inside the resources/views directory.
resources/views
Laravel uses Blade templating engine. So, the welcome view code is inside the resources/views/welcome.blade.php file.
welcome
resources/views/welcome.blade.php
We will talk about views in the views tutorial.
We will create new routes for our todo-laravel project using the following methods.
And here are the routes.
/** * Get the ACTIVE todo tasks. */ Route::get('/', function() { // code to fetch todo tasks }); /** * Get the ACTIVE todo tasks for a given page. */ Route::get('/todo/active/{page?}', function($page = 1) { // code to fetch the todo tasks on page = $page }); /** * Get the DONE todo tasks for a given page. */ Route::get('/todo/done/{page?}', function($page = 1) { // code to fetch the todo tasks on page = $page }); /** * Get the DELETED todo tasks for a given page. */ Route::get('/todo/deleted/{page?}', function($page = 1) { // code to fetch the todo tasks on page = $page }); /** * Get a specific todo task by id. */ Route::get('/todo/{id}', function($id) { // code to fetch todo task having id = $id }); /** * Create a new todo task. */ Route::post('/todo', function() { // code to create new todo task }); /** * Update a specific todo task by id. */ Route::put('/todo/{id}', function($id) { // code to update todo task having id = $id }); /** * Delete a specific todo task by id. */ Route::delete('/todo/{id}', function($id) { // code to delete todo task having id = $id });
Note! At this moment the routes will do nothing as they have no code.
In the coming tutorials we will dive deeper into each of the routes.
Alright, see you in the next tutorial. Have fun coding :)