Back to Top

Understanding of Laravel Routing and views

laravel-routing-and-views

We have already seen the installation steps of laravel. It sounds like you already level up your skills of laravel.Laravel emphasize simplicity, cleaner, and performance.This article is about to give you an understanding of Laravel Routing and views.

It is kind of overwhelming at first to learn laravel so I am not going to explain all of the functionality at once. I am dividing it in a piece of articles starting with basics and go deep into complex functionalities.

In the first step of learning, let’s start with the routes.

What is Route in Laravel?

Laravel routes are used to define your route files which reside in the routes directory.web Routes is where you can register all your web routes of your application.The routes/web.php file defines all your routes and gives web interface. Routes are mainly used to describe how you want to routes any incoming request.

Basic Routing with example:

Here is the basic sample code which you can see in routes/web.php file.

It looks like you have Route class which makes get a request to home page then it will trigger the anonymous function on that page and return the output. This is a very basic concept used for the small application which will directly call the view file. You don’t need to call the controller if not needed.

If you are developing a complex project, you might need to familiar with the concept of the controller. Let’s see how you can call controller in Laravel as below:

It says when you make a request to the banner page, load BannerController@banner. The only difference between this two is, in the first example, you can define routing inline and in the second you are loading a dedicated class to process the routes.

What is view in Laravel?

The view is the interface what end user sees.Here you can use a view to define HTML structure. You can call html or php file whatever is appropriate. Views files reside in the resources/views directory.You can default welcome.blade.php file in views. In laravel, it is not necessary to pass full file name view(‘welcome.blade.php’), you can just write view(‘welcome’) and it will work.

If you look at the welcome file, you can see inline CSS, html code. If you want to see this in the browser, Go to browser and run http://domainname/ (http://domainname/). It will run your welcome page. You can use simple string instead of welcome template like

And go back to the browser and refresh, it will display string.

What is artisan in Laravel?

Laravel artisan utility is command line component which gives a number of commands to do any number of things. for example, if you want to create a controller using the command, you can run php artisan make:controller . So you can clear cache, generate any file using artisan command.

Let’s create custom routes in Laravel:

Let’s tried out this code and it will render the string in the browser. This is the simplest way to routes string on the page.This will not work in real time application. You need a view to display your all content of the page.

This code will look for file resources/views/aboutus.blade.php file so you need to create a file in that and write you aboutus code in that. You can not put all files directly into views directly while working on the complex application.You might want to create nested folders as well. At that time you can create any directory. For example resources/views/pages/ directory so how you can notify this? You can use the dot(.) notation.

This code will look for file resources/views/pages/aboutus.blade.php file. you can use “return view(‘pages/aboutus’);” but dot notation is better to look little more object oriented way. if you don’t know about blade then it is just laravel template engine.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

Learn React JS with simple Application

Posted on 7 years ago

Bhumi

How to use session in Magento

Posted on 11 years ago

Bhumi

How To Install Bugzilla on Windows

Posted on 11 years ago

Bhumi

What is Final keyword in PHP

Posted on 12 years ago

Bhumi