TECHNIG
Gateway for IT Experts and Tech Geeks

Laravel 5.2 Basic Routing

Previously, we learned how to install and configure Laravel framework. And now we are going to continue our topic about how Laravel routing and views works. If you haven’t install Laravel yet, I recommend you to review our previous topic about how to install laravel 5 on windows, both using composer and windows installer.

What is Laravel routing ?

Simply, routing is just how Laravel respond to URL’s request. All Laravel routes are defined in the app/Http/routes.php file, which is automatically loaded by the framework. Routing is the core components of Lararvel framwork. It looks a little complex at first, but once you get comfortable with basics you will be amazed by this feature of Laravel.

Before starting, go to your Laravel root directory and open command line, than write the command “php artisan serve” (without quote). This command will run your web server on port 8000.

Laravel routing and php artisan serve
Laravel routing and php artisan serve

Now you can access your Laravel project at “http://localhost:8000/“.

To work with Laravel routs, you should open app/Http/routes.php file in your Laravel project and. All Laravel routs are written there.

Imagine if you have: Home, About and a Contact page for your site. The users might look for those page like this;

for Home page:

http://localhost:8000/

for About page:

http://localhost:8000/about

and for Contac page:

http://localhost:8000/contact

So in Laravel routs you file, you can write it like this;

// For Home page
Route::get('/', function () {
    return view('home');
});
// For About page
Route::get('/about', function () {
    return view('about');
});
// For Contact page
Route::get('/about', function () {
    return view('contact');
});

The above code is just basic way of routing in Laravel.

The Class Route has a static method called get(), which accept two values or arguments. The first one is the directory, and the second one is the function which will be called after the user call the specified directory.

I real world applications, you will not call the views directly from routes, instead you will call the controllers and the controller will call the targeted view.

Laravel Route Parameters

Of course, sometimes you will need to capture segments of the URI within your route. For example, you may need to capture a user’s ID from the URL. You may do so by defining route parameters. let’s do learn it by writing an example.

You might want to capture students name from url:

// get the parameter of name
    Route::get('students/{name}', function($name) 
    {
        return 'Students Name is ' . $name;
    });

Now in your browser, you can access the student name like this “http://localhost:8000/students/ali”.

Laravel Optional Route Parameters

For this example, let’s say we have a gallery of photos. We also have categories of photos. The category will be optional, if the user set the category, it will show all images from that category, if not it will show all images.

// optional category
    Route::get('gallery/{category?}', function($category) 
    {
        // if category is set, show the category
        // if not, then show all
        if ($category)
            return 'This is the ' . $category . ' section.';
        else 
            return 'These are all the photos.';

    });

Conclusion

So, its was just basic and starting of Laravel routing. I hope it has been informative for you, if you have any question of you want say anything about the tutorial feel free to comment it below, and I will join the discussion. 🙂

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

where to buy viagra buy generic 100mg viagra online
buy amoxicillin online can you buy amoxicillin over the counter
buy ivermectin online buy ivermectin for humans
viagra before and after photos how long does viagra last
buy viagra online where can i buy viagra