Laravel Routing

one of the first steps to start your project is to create routes.

if you are familiar with apache mod_rewrite, then you will become very familiar with laravel routes.

routes are the url attributes you will be using in your project. by now, you should know how to start your development server with http://localhost:8000

lets say for example, we want to create a page with 'users', this page will display the users profile for the website so we need the url to be something like this:

http://localhost:8000/users/el_lara

lets break this url down:
http://localhost:8000= the local server root
/users = module
/el_lara = username

first lets start by creating the users route, i will list each directory and file name:

Example 1 - Return Text


Add to routes/web.php
Route::get('users', function () {
return 'Hello, This is the users route';
});

http://localhost:8000/users Output: Hello, This is the users route

Example 2 - Return String Value


Add to routes/web.php
Route::get('users', function () {
$string = 'Hello, This is the users route';
return $string;
});

http://localhost:8000/users Output: Hello, This is the users route

as you can see, example 2 return the same message, except you are using a string value. you can return whatever you want, even an object;

Example 3 - Using Controllers


but the best way to use routes is by using a controller.
you can create a controller manually or through the artisan command.
to manually create a controller, create this file in the app/Controllers/ directory, i will call my file UsersController.php

UsersController.php should look like this:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UsersController extends Controller
{
//
}

or use this command (preferred method) to automatically create a file with the code above
php artisan make:controller UsersController
this command will create a file called UsersController.php in app/Controllers/ directory
open UsersController.php - you will see artisan created some code for you. move the code from inside the Route::get('users', function () in routes/web.php, inour case, this is the code:
$string = 'Hello, This is the users route';
return $string;

UsersController.php should now look like this:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UsersController extends Controller
{
public function index(){
$string = 'Hello, This is the users route';
return $string;}
}


now go back to /routes/web.php file and change the 'users' route code to:
Route::get('users', ['uses' => 'UsersController@index']);


refresh browser to http://localhost:8000/users
Output: Hello, This is the users route

as you can see, the output is the same, however, now we are using a controller.


Using Blade

now that you have created the UsersController, you can use laravel blade. open app/Controllers/UsersController.php and change:
FROM:
return $string;

TO:
return view('users.index', compact('string'));


app/Controllers/UsersController.php should now look like this:
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UsersController extends Controller
{
public function index(){
$string = 'Hello, This is the users route';
return view('users.index', compact('string'));
}
}


create a new directory called users in resources/view,
create a new file index.blade.php in the users directory and open a new blade file: resources/view/users/index.blade.php
add this code to ndex.blade.php :
<h1>{!! $string !!}</h1>


refresh browser to http://localhost:8000/users
Output: Hello, This is the users route

for more information, you can visit the official documentation at: https://laravel.com/docs/5.3/routing

if you prefer, here is a video