Introducing Laravel: Routing, Blade and Laravel Mix

In our last post on 'Introducing Laravel' we looked at installing and setting up our application. To follow, we'll start adding some logic into our new project by utilising Laravel's routing system.

In addition, as Laravel is a MVC (Model, View, Controller) based framework we'll be looking at the V or View side of that, by experimenting with the Blade templating engine and Laravel Mix, a new addition to Laravel in it's latest 5.4 release.

This is the 2nd part of my Laravel series of posts. Check out the others from the links below once they're posted:

Disclaimer

If you're new to the framework or the object-orientated land of PHP then working with Laravel may seem overwhelming at first. There may be some sections of this series that you're not too clued up on but stick with me, I'll be breaking things down into bite-sized chunks which should provide you with those illustrious "a'ha!" moments that everyone craves.

That being said, this series does expect a basic understanding of PHP or similar programming languages.

With that, let's get started.

Routing

In programming, routing is at the heart of every website, whether you know it or not. No matter their functionality, all websites must have some form of routing behind the scenes in order to work, whether this be at a server level, application level, or in most cases, a mixture of both.

Routing is the practice of taking a URI endpoint and converting it into parameters to determine what code should be ran.

Aside from the domain, which directs our users to the correct server, we have the following endpoints to work with.

/posts/introducing-laravel-part-2-routing-blade-mix

A routing system will take these endpoints and break them down into bitesized chunks for the application to work with in a controlled environment. For my example, I've setup the Assortment blog so that it knows:

Not only do I want to find a post;

But I want to find a post that has the slug of introducing-laravel-part-2....

All frameworks and Content Management System's will have their own way of doing things, but I'm a big fan of Laravel's. Here's why.

Laravel routing

Laravel's routing system is designed in a very developer friendly way and if you're coming from the land of Codeigniter or other PHP frameworks where it's routing system is scattered throughout the codebase then you're going to love the fact that Laravel simplifies this into one central location.

If you don't already have a Laravel project installed from the last post then go do that now. Once installed, locate the routes folder within the root directory of our project. This folder's entire purpose is–as you might expect from it's name–to house all routing logic for your project.

In the folder are four separate files, each with their own purpose. For the majority of applications you'll be concerned with either web.php or api.php but here's a quick primer on each:

web.php - Where you can register web-based routes for your application.

api.php - Where you can register routes for API endpoints.

console.php - Where you can register commands to be used through your Command Line Interface (CLI) for things like deployment scripts and testing.

channels.php - Where you can register broadcasting channels for your application to be used in conjunction with Websockets, creating true, realtime content streams for the web.

As we're looking at creating a little web app, we'll stick with the web.php file for now. Open up routes/web.php and let's get started. As of version 5.4, here's what that file looks like.

To declare a new route in Laravel you must use the Route class, followed by one of the available methods it has at it's disposal. The more commonly used methods mirror HTTP requests such as Route::get for a GET request, Route::post for a POST request and so forth.

These methods accept two parameters, a URI endpoint and a Closure, where the closure should return what you'd like the user to see when they visit that route.

In our web.php we currently have a GET route setup to look for the URI endpoint of /, meaning the homepage or root of the website. Once a user visits that URI we return what we would like them to see, in this case we use the view function which loads a template from the resources/views directory. This means anytime a user goes to the homepage of our project we provide them with the resources/views/welcome.blade.php template.

Get that? Cool, we'll use it as a basis to create a completely new page for our dummy project. Underneath our welcome route call the GET method on our Route class again.

Route::get();

Next, add a URI endpoint of about to the first parameter as we're going to create an 'About us' page for our project.

Route::get('about');

For the second parameter, create a Closure which for the time being returns 'Hello world'.

Route::get('about', function() {
return 'Hello world';
});

If you go to your browser and visit the /about page you'll now see the 'Hello world' message.

Fig 1. Demonstration of a new 'About' page route within our Laravel application

That's great, our route is now working, but we do need to polish things up. Replace the 'Hello world' message with the view function calling a template named about.

Route::get('about', function() {
return view('about');
});

We now need to create that new template. Within resources/views create a new file called about.blade.php

Notice the .blade.php file extension? That tells Laravel that we want to use Blade, a templating engine baked into Laravel giving us some cool goodies to work with. More on this later.

Within the new file, add a message that says 'The about page is now generated from a template within resources/views. Yey!'. If you now visit the About page in your browser you'll see that message popup. Awesome!

Fig 2. Demonstration of the same 'About' page route using a View template

Templating

Now that we have a template we can start adding some HTML to provide some structure and layout to our page. Replace the contents of about.blade.php with the following HTML. Nothing fancy, just some basic formatting to give the idea of a fleshed out design.

Blade templating engine

Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.

Within resources/views create a new file called app.blade.php. This template will be our layout dummy, storing the HTML used in multiple pages to reduce duplication. In Laravel and many other frameworks, this is known as a master layout, which all other templates are derived from.

Notice the @yield() commands in both the <title> and <body> tags? In Blade, this creates a placeholder for other templates to use. Whenever a template extends a master template such as this, they must fill in these placeholders.

Running through this file, the @extend('app) command tells Blade that we would like a slave template based off a master template, in this case the app.blade.php. Once that's confirmed, we fill in our placeholders with a title and content area specific for the homepage.

Let's do the same for the about.blade.php, by replacing it's contents with the following.

If you load both of these pages up in the browser you'll notice they're the exact same, yet we now only need to edit the majority of our HTML in one place, our app.blade.php master template.

Laravel Mix

Now that we have our master and slave templates setup, we need to tackle the CSS. Laravel handles this out of the box too thanks to Laravel Mix.

Most modern projects require the use of build tools such as Gulp or Grunt to automate their compiling of assets, amongst other things. In Laravel 5.4, Laravel Mix was introduced as a successor to it's predecessor, Laravel Elixir to provide "a fluent API for defining Webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors". In layman's terms it gives you a simple way of defining how your Sass is compiled to CSS, your brand-spanking JavaScript into browser compliant code and much, much more.

Before we get started, please make sure you have Node installed on your machine. Run npm -v in your Terminal application to check. If not, you can download it on the official website.

Once you've confirmed you have Node installed, within the root of your directory install your NPM dependencies.

$ npm install

Note: If you're on a Windows system or running a Virtual Machine, then you may need to append the --no-bin-links flag to the end of that command.

Once installed, we can run the various tasks that are visible in your package.json file in the root of your directory. As we're currently in development, let's run the dev command

$ npm run dev

This will run all the Laravel Mix tasks, however, if you're also looking to minify your output then npm run production is for you.

Fig 5. Output for the dev command in Laravel Mix

So what has that actually done? Well, if you take a look in resources/assets you'll notice the two folders js and sass. The NPM script you just ran compiled the files within there into two single files over at the public directory.

As Laravel Mix comes out of the box with the full Bootstrap framework, we can start using that to scaffold our dummy project.

Within our resources/views/app.blade.php master template, remove our current styles within the <style> tag and replace it with the following link to our new stylesheet.

<link rel="stylesheet" href="{{ mix('/css/app.css') }}">

The double curly brackets {{ }} are a syntax specific to Blade similar to the opening (<?php) and closing (?>) PHP tags. Notice the mix() function between those brackets? Rather than manually writing the entire path to our CSS, we can use this function to find this file from within our public path, whilst also giving Laravel Mix the ability to do some fancy shenanigans such as cache busting in the future.

You can see already that the templates could start getting messy. By creating partials for our different components and using Blade to bring them in we can remedy that. Within resources/views create a new folder called partials and create a file called navbar.blade.php. Move the navbar component code from app.blade.php and place it in here. In it's place, add a Blade include to load in that new file.

Again, you'll notice nothing has changed other than your files are much easier to read. Finally, going back into the navbar.blade.php partial, we can finish up by creating better links to our routes. Currently we have the link for the homepage set as href="/" and the link to the about page set as href="/about". As Laravel may not always be in the root of the directory, it's best to use Laravel's url() helper function to define any links for the time being.

A little explanation on a few areas

Naturally with a series like this I won't be starting off with the absolute production ready code and there may be some areas in this post that you feel could be done better, and I'd agree. Please remember that this is an introductory series and as such I do not want to overburden people who are completely new to Laravel.

That being said, here's a few things I'd like to address in future posts.

Parameters for Route::get() and similar

In the post I elude to the idea that there are two parameter's for the various HTTP methods available to the Route class. Whilst true, rather than provide a Closure for the second argument you can instead reference a Controlller. We'll be covering this in the next post.

Usage of the url() helper function

Once we have controllers setup we'll be looking into the action() and even the route() functions once we have named routing setup. Again, one for a future post.

Concluding thoughts

We've touched on a lot of core topics for Laravel in this tutorial and there's much more that could be said about each. If Laravel is a framework you're eager to research I'd definitely recommend giving the official documentation a good read through and a subscription to Laracasts would do you wonders.

In the next post in this series I'm planning on introducing the concept of Controllers and how databases are configured in a Laravel project. Got any suggestions, I'd love to hear about them, please leave me a comment!

SCItano

This is may depend from implementation front-rank equipment park,technology,equipment,algorithm,cleaning,methods,equipment,components,mechanisms and industrial equipment, specialized detergents and cleaning funds and also beautiful organization work cleaning service.
In our world use support Cleaning firms made necessarily authoritative and relevant.

Some enterprises and organizations Partnership long for a time enjoy help with cleaning companies, but not conclude personal frames swabber for such types of services as Cleaning a private house and Bathroom cleaning in TriBeca .

You will be spared from wasting time on training and recruitment frames , on check his work, you it will not be necessary to allocate room for cleaners and organize storage inventory.

We Limited liability Limited Partnership is that is on the street Douglaston invite you personally work with uson profitable conditions.
Any Economist Proves You contain Multiple workman at a loss in collation with employ foreign cleaning service.

Linzacebra

ShaylaDrelf

Incredible update of captcha regignizing package "XRumer 16.0 + XEvil 4.0":
captcha breaking of Google (ReCaptcha-2 and ReCaptcha-3), Facebook, BitFinex, Bing, Hotmail, SolveMedia, Yandex,
and more than 8400 another types of captcha,
with highest precision (80..100%) and highest speed (100 img per second).
You can use XEvil 4.0 with any most popular SEO/SMM programms: iMacros, XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software.

Interested? There are a lot of impessive videos about XEvil in YouTube.

Dear customers! we offer chip-tuning, professional ECU software remapping, DPF / FAP / CAT removal, EGR / VSA removal, ADBlue / NOX removal and DTC removal services in most car ECUs. Our expirience in this theme - more than 6 years. Really fast terms of work with high quality and warranty for good price only from us!
We offer professioinal and safe software remapping and DPF / FAP, EGR, CAT, O2 sensors, VSA, MAF / MAP, VBA, NOx, Adblue and DTC removal service for most popular ECUs. We have tested and finished a lot of solutions for many cars, commercial trucks and agricultural vehicles and now they work perfectly! We can do calibration of ECUs from most manufacturers, like Bosch, Siemens, Sagem, Marelli, Melco, Mitsubishi, Denso, Delco, Continental, VDO, Johnson controls, Valeo, Visteon, Hitachi, Kefico, Keihin, Matsushita, Motorolla, Janvar, Itelma, ABit for any car model. It isn't difficult and it is quite safe to program ECU with some basic knowledges. Firstly, you should read original ECU ROM via compatible device (like Kess2, FGTech2 Galetto, Piasini, MAGPro, KTAG, CMD, MMCFlasher, etc) on the car or on the working bench or with ECU disassambling. You need to send original file from car ECU (in open binary format) with needed task to us. So, we need only 30-60 minutes for standard file modification (example: for removing DPF and EGR). Chiptuning takes more time, but not more than 2 hours (with standard ECUs and tasks). After our work is finished we will send modified program back to you and then you should write this file in ECU.

We use ONLY ORIGINAL licensed tools in our work! It very important moment for clear work and safety! We have a many partners in Russia, SNG, EU, USA and other countries and we hopes on long and mutually beneficial cooperation with you. Also we can help with selection and purchase of special chip-tuning and diagnostic equipment.

mobile game

Ethan

Great post! I really appreciate the way of explaining with images. Thanks for sharing with us! you can even create your own free-to-play game application with the help of top rated game firms. All you have to do is explain to them what type of game you want to develop, they will make a 3D model with the best user Interface. Android game development India is making voice controllable games with the proper player experience.