Todo Laravel Project - Routes

Laravel Project

In this Laravel project tutorial we will learn to create Routes.

The route file

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 are used to map URLs to controllers or anonymous functions.

The web.php route file

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.

Views are saved inside the resources/views directory.

Laravel uses Blade templating engine. So, the welcome view code is inside the resources/views/welcome.blade.php file.

We will talk about views in the views tutorial.

Creating new routes

We will create new routes for our todo-laravel project using the following methods.

  • GET - To fetch tasks
  • POST - To create new task
  • PUT - To update an existing task
  • DELETE - To delete an existing task

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 :)