Home / Blog / How to create a website with Laravel?
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading...

How to create a website with Laravel?

The Laravel framework is the best solution for quickly and easily creating a web project

 

It allows you to always be in the trend of modern web development techniques. Although, Laravel belongs to PHP frameworks there is no need to underestimate it. After all, it is a highly developed ecosystem of products.

 

According to the opinions of programmers, high-class developers should always increase their skills in a notepad. However, there is a big difference between memorizing variable names and optimizing the web development process. This is where the potential of the framework is revealed. After all, the main task is to master the entire IDE functional.

 

In addition to the integrated development environment, you must install Composer. With it, not only. Laravel will be installed, but additional packages will be added. At the same time, it is necessary to adhere to the correct order of installation of the framework.

 

In conclusion, Composer will become an important and truly useful tool for web development.

 

The following paragraphs of this article will not describe the process of writing code. We will consider further actions after installing IDE and Laravel. After the framework installation, the display of the base page is written in the code. So, you can go to the next step.

 

Next, you need to make a code testing

 

To test a web project, you do not need to upload files to an FTP server, install local Apache (the same Denwer or XAMPP) — this was done many years ago, and many newcomers still do this. This is wrong and will not save you from errors in the code. To date, there are appropriate tools for these tasks that will save a lot of time and nerves.

 

Laravel offers us to install Homestead. Homestead is an image of the Ubuntu operating system that already has everything you need installed. You can familiarize yourself with the installation and configuration of Homestead here. To install the image, we need Vagrant and VirtualBox. Thanks to this image, you will know exactly which modules you need to install and how your code on Ubuntu will behave. You can also install any additional software. In short, your system will have public folders with code that will be available inside the Ubuntu image, and your code will be executed exactly inside Ubuntu. In the browser, you type site.the app and your site are displayed from Ubuntu. At the same time, you will also have access to Ubuntu via SSH. For beginners, installing and configuring Homestead will take some time, but as a developer, you simply must. It is worth noting that Homestead can be installed not only on Linux but also on Windows.

 

Next, we assume that Homestead is installed, and the site with the latest version of Laravel opens in your browser

 

Your code runs in a browser, but does it really work? Should not write code without tests. It all works as we intended. Take the time to write tests. Every professional developer must write tests of his code.

 

Laravel offers the tools to fully test a web project with all parties. You can test everything: create a temporary database, check the completion of HTML forms, check file uploads, even the contents of PHP sessions and sending emails. Laravel was created for quality testing of all the features of your project.

 

Testing documentation can be found here

 

In Laravel, tests are located in the tests folder and phpunit commands are executed in the console, or immediately from the IDE. There are several types of tests:

  • Functional — Feature-tests
  • Modular — Unit-tests
  • Feature-tests — functional tests.

 

For example, registering users, sending notifications, filling out web forms, downloading files. They allow us to check the data in the browser. To find out if they work. You can test using Laravel Dusk, and not just send HTTP comments using the real Google engine. Laravel Dusk will help us with this.

 

Another type of testing is called unit testing. Tests were tested to ensure that all applications could be broken if you or someone else failed to change your code. All tests must run successfully.

 

When changing the functionality, you can add tests. This will save you and your colleagues from mistakes and help diagnose the problem easier. Unit testing avoids errors in the application logic. It is worth noting that there is a development technique for TDD (test-driven development) — development through testing. First, we write tests, and then we gradually implement the code.

 

When all the tests are completed, we can say that we have finished writing the code. If you have not written tests for your projects, then it’s time to move to a new level. In addition to tests, there are other helpers for analyzing the performance of a web application. Laravel offers us to install the Laravel Debugbar. This is a special package that is displayed on your site in debug mode. Using it, you can track all the SQL queries to your database with the goal of further optimization.

 

After creating a web project and passing the tests, we need to prepare our project for placement on the server

 

Laravel provides us with the Laravel Mix. Laravel Mix uses Webpack and can work with CSS, JS, Less, Saas, Stylus, PostCSS. This is a wonderful tool that, using a special builder of Webpack modules, collects all our JS and CSS files together. Most importantly, it can create new versions of these files. Thus, each assembly of our project allows us to have different names for JS and CSS files in HTML code, which solves the problem of caching when changing the contents of a file.

 

In the template of our project we need to write this:

<link rel=”stylesheet” href=”{{ mix(‘/css/app.css’) }}”>

 

After compilation, it turns to:

<link href=”/css/app.289df32d2d2c47df3b16.css” rel=”stylesheet”>

 

In this case, the visitor’s browser will immediately download a new file from the site. Isn’t that convenient? Similarly, with JS files.

 

It is worth noting that Laravel works great with the progressive Vue Javascript framework and makes it very convenient to create web applications based on this JS framework. In addition, each component can be conveniently placed in a separate file. You can read about how to write components for Vue using Laravel at this link.

 

 

Usually, after building a project, its files must be uploaded to the server and the table structure in the database updated. We take a folder with files and upload them to an FTP server. Then, we go into phpMyAdmin and make changes to the database. We will not use FTP and phpMyAdmin, otherwise, while we are making changes, all users who visit the web project website will see many errors about the absence of any files or fields in the database.

 

Of course, we can announce the completion of technical work, showing our users how technically complex our project is, which requires a complete shutdown, but it certainly will not please anyone. There is a very simple and competent solution that allows you to update the code of a web project without disabling it, and no user will receive an error message.

 

The first thing we need to learn is Git. Git is a distributed file version control system. With Git, you can track changes in files, return the old version of files and work in a team on the same code, without confusing anything. You can use Git through the service and create either a public code or a private one (it’s paid for private repositories). You can also use another free BitBucket service, which allows you to create private repositories with a code for free. In addition, Git itself can be configured so that when making changes, certain actions occur:

  • running project tests through Travis CI;
  • formatting code according to the standard;
  • code quality analysis through the tool.

 

Thus, all the code for your web project will be stored in Git, it will always be of high quality and proven. For example, if you offer to make changes to the official code of the Laravel PHP framework, then when you make changes, tests that check the framework’s work automatically take into account the new code.

 

Earlier we talked about the process of deploying a web application. That’s why we need Git. From your local machine, you upload the web application code to Git, after which the deployment of the application to the server will automatically start. Laravel Forge is a server without hassle.

 

For automatic deployment from Git, the Laravel Forge service will help us

 

 

Through Laravel Forge, you can create a virtual server in DigitalOcean, Linode or specify access to your own server. In this case, absolutely all the necessary software will be configured for the Laravel PHP framework to work. Laravel Forge automatically installs security updates. Forge also easily installs a free SSL certificate from Let’s Encrypt. You can give the Laravel Forge service access to your Git repository, and with each change in the code on the server, its latest version will be automatically deployed. Do you want to have 10 servers? No problem, Laravel Forge can install a load balancer, create 10 virtual servers, copy code from Git to each server and run the project.

 

Do you think that’s all? No, together with Envoyer you can launch new code into operation without stopping the service at all

 

Here is a small script in the Laravel Forge panel, which runs every time the code is deployed and provides on-the-fly replacement while keeping a few more copies of the old code on the server itself.

 

So, we finally created a comfortable working environment:

 

installed the IDE, Composer, Laravel PHP framework, wrote the project code, ran the tests, studied the Git version control system, sent the code there, connected the Laravel Forge service, if desired, also connected Laravel Envoyer, made the project deployment to work server from our git repository.

 

We can say that Laravel directed us on a competent path in web development. There is still a lot to learn, but we have already done a great job and can begin to work in a team with other developers. Laravel itself is constantly being improved and follows modern trends. By studying it, you will not lag behind the world of web development, the main thing is not to focus on any particular version of the framework but develop along with it. To do this, you also need to study the innovations of Laravel.

 

I will try to describe the main features of Laravel so that you can evaluate the scale:

  • MVC (English Model View Controller) The PHP framework is based on the well-known and reliable Symfony components;
  • The necessary modules for the framework are connected in the form of service providers. In Laravel 5.5, it’s easy enough to install the package through Сomposer, and it will be immediately available, without having to write anything in the code;
  • The framework code is separated from the developer code, each component is easily extensible;
  • Web project code, CSS, JS, HTML-code of pages are divided into separate directories. The framework uses the wonderful Blade template engine, which allows you to separate the layout from the PHP code. The template engine itself is so simple that even a novice HTML layout designer can easily overpower it;
  • Convenient routing, validation of incoming parameters;
  • Caching, work with file storages, work with various databases;
  • Huge opportunities for testing a web project, including filling the database with test data;
  • The framework even has its own site with a package library.

 

With Laravel, you can generate a system of registration and login to the site with one command and easily connect OAuth authentication services thanks to Laravel Socialite or even create your own using Laravel Passport

 

 

For those who don’t know about OAuth, it’s an opportunity to enter the site through social networks. This is only a small part of what Laravel can do, and if a novice PHP developer explores its capabilities without even delving into the core of the PHP framework itself, it will certainly raise his level of knowledge not only in Laravel development but in web development in particular, including teamwork skills and an understanding of the principles of developing highly loaded projects. It’s not without reason that there is a motto on the main site of the Laravel PHP framework: “Love beautiful code? We do too.” After all, the code of the Laravel PHP framework is not only beautiful, pleasantly readable, but also very well thought out, and a lot of people think about any change, which allows you to create professional web applications at the master’s level.

Similar Posts