Where we take good care of both your front and back-end needs

Great place to take your code to the next level

Checkout my work and get in touch

RastcodeS Logo
Go Back

Laravel Routes and Controllers - How I made this app Part III

Artisan Git Laravel PHP

Written by Rastko

date - 25/Aug/18 | views - 880

Laravel Routes and Controllers - How I made this app Part III

     Introduction to Laravel routes, controllers and the Blade templating engine. You'll get to know how to make new Blade pages, which extend layouts, how to make routes and controllers for them. We'll also mention a few useful Artisan commands.

     This is a third article in a series - How I made this App. If your haven't already, make sure to check out the previous two:

Laravel Starter App - How I made this app Part I

Laravel Folder Structure - How I made this app Part II


Laravel Routes


     The first two articles were a rather big setup for our app, but I found them necessary as we won't have to go back as much to fix things that don't work. Now it's time to start coding.

     Routes in Laravel, or any other web framework, allow us to register any available path in our application and declare which functions handles them. In Laravel the most simple route includes a URL and function ( Closure ), where we decide what should be returned. Let's look at our web.php file under routes directory:

Route::get('/', function () {
    return view('welcome');


     As you can see a Route uses a method get which takes the URL, in this case '/' - the root of our app. The second parameter is a function, which returns a view named 'welcome'. That's that welcome.blade.php file, Laravel automatically adds the .blade.php part. That's our Laravel welcome page. We'll actually get rid of it in a second, as we want to create our custom home page. You might be asking, does Route have other methods besides get? Of course it does, the usual suspects are all here:



     We'll discuss these later, but first let's make a new route to our brand new page - about.

     In the routes - web.php file add:

Route::get('/about', function () {
	return view('pages.about');


     If you try to access this page now by typing yourappname.me/about in your browser, you'll be greeted by this:

Invalid Argument Exception Laravel


     That's because we haven't created that page yet. Let's do that now. In resources - view folder create a new directory named pages and inside it a new file - about.blade.php. That's why we returned pages.about instead of just about. It will come in handy later.

     We'll get to the cool Blade features later but just know that you can write plain HTML code here. For now I'll just add a heading and a simple paragraph:

{{-- About Page --}}
<h2>About Us</h2>
<p>Hello, this is a great place to get to know more about us!</p>


     Oh, by the way that's what Blade comments look like, at the top. If you now visit our about page - you'll finally get the content. The integrated bootstrap styles don't work, but don't worry about it. We'll actually make a layout, which will be extended by most of our pages and that's where we'll include our styles and scripts.

     Let's get back to our web.php file and find out how we can even easier return a non - dynamic page. For this example - we'll make a new services page as well. In web.php add:

Route::view('/services', 'pages.services');


     View routes are very simple and are often used when you just want a return a view without any special programming.

     And in our new pages folder create a new file - services.blade.php:

{{-- Services Page --}}
<h2>Our Services</h2>
<p>Hello, this is where we tell you about all of the great services we provide for you!</p>


     Ok, so we have our new page and it's great, but we are getting a lot of clutter. It's time to introduce controllers, which will not only help us tidy up, but also later allow us to add greater functionality to our  pages. Before we move on though let's try our first Artisan command. Artisan is a command line interface, included with Laravel, which provides us with many useful commands to ease our development. For example, we created all these great routes, maybe we can get artisan to show them to us:

php artisan route:list


     There are all of our routes, with an additional one, but we'll talk about that later. If you want to play around with artisan, this command we'll get you a list of all available commands and flags:

php artisan list


     You can also use the help option before any command to get additional information:

php artisan help route:list


Laravel Controllers


     Controllers are very simple at first glance. For example, a user clicks on a link, the server intercepts this request and asks who can handle this? In our case the web.php file will give the answer and point to a designated controller and a specific function to handle the request. This function in the controller will then, if needed gather data the request needs, and return a response. In our case the response will be one of our cool views.

     Before we get to that, let's make our custom home page instead of the provided welcome. We'll do a similar thing we did with about and services. In views folder add a home.blade.php file:

{{-- Home Page --}}
<p>Hello, this is our brand new website!</p>


     Ok, now that we have our 3 main pages let's make our first controller. Again artisan comes to the rescue and will help us avoid writing some boilerplate code. In a command line interface run:

php artisan make:controller PagesController


     This is very simple, we tell artisan to make a new controller and name it PagesController, but wait, where is it? You can find your new controller under app - Http - Controllers. If you open our newly create file, you'll see something like this:


namespace App\Http\Controllers;
use Illuminate\Http\Request;

class PagesController extends Controller


     Let's go back to web.php file for a second and tidy up a bit:

// Basic Routes
Route::get('/', 'PagesController@getHome');
Route::get('/about', 'PagesController@getAbout');
Route::get('/services', 'PagesController@getServices');


     This is what we want the file to look like. We still have our URL-s, but now we have a few new things under single quotes. As you can guess, each of these tell us that the request to the specific URL will be handled by the PagesController and a designated function ( getHome, getAbout, getServices ). We haven't made these methods yet so let's do that so they can handle our requests. In PagesController:


namespace App\Http\Controllers;
use Illuminate\Http\Request;

class PagesController extends Controller
   public function getHome()
      return view('pages.home');

   public function getAbout()
      return view('pages.about');

   public function getServices()
      return view('pages.services');


     Hopefully, your PagesController looks like this and even though you might be wandering why we added more code to do the same thing, it's actually going to help us in the long run especially when we add some dynamic content.


     All that's left to do is add our changes to the Git repository, make a new commit and push it all to GitHub. Be sure to check the previous chapter - Laravel Folder Structure - How I made this app Part II - if you want to learn how to make a fresh repo.

     Add changes:

git add *


     Make a new commit:

git commit -m "Created PagesController, new pages and routes"


     You need to add a message, although you can put whatever you want in it.

     Finally push to GitHub

git push


     If you want to check out the repo used for this guide go to Laravel Blog Tutorial


     That's all for this part. I hope you enjoyed it. Next time we'll focus on Blade and how you can make layouts and extend it to your pages. We'll also explore some dynamic features of the templating engine and create a navigation menu, so that we can have a cleaner user interface.

View App/Backend
Login to comment


No Comments yet. Be the first!

Currently working on a Laravel project

Check it out
Java Break - Declare and Initialize Variables - Java Core Part - VI
Java Break - Declare and Initialize Variables - Java Core Part - VI

     Declaring and initializing variable in Java. How to do it properly and avoid mistakes.

     The sixth chapter...