Laravel is a free, open source PHP framework, considered one of the best PHP frameworks. He has a great community, a fantastic documentation site, and great video tutorials. Version 1.0 was launched just a few years ago. By installing Laravel, you can create and maintain high-quality web applications with minimal workloads.
Briefly about Laravel
Laravel is a collection of general purpose components and libraries combined with programming standards that simplify the development process. The main advantage of installing Laravel is its flexibility. Most frameworks force a programmer to work in a certain way. Laravel takes a slightly different approach. He does not force the style, but only offers it. Thus, the method combines all the advantages of standardization with the ability to add additional functions.
Today, the installation of Laravel is very popular. This framework is based on data provided by HotFrameworks. Laravel takes 8th place in the overall ranking and 1st place in the ranking of PHP-based frameworks with more than 23 thousand stars on GitHub. For comparison, we note that CodeIgniter is only in 2nd place with 12 thousand stars.
Technological documentation and installation
As with every new technology, users should start exploring Laravel by reading the documentation. Now Laravel 5.7 is the most stable version, and acquaintance begins with the official site. The Laravel installation documentation is neatly structured. It is worth mentioning that it is sometimes useful to look at older versions of the documentation.
Laravel documentation suggests using Homestead is a good long-term solution, but it takes a lot longer to implement. After creating new servers, the struggle goes on every second, so it’s easier to stick to an alternative solution: Artisan. It is the main tool in working with the Laravel framework for many users.
One of its functions is to start the server with the "./artisan serve" command. If you need to stop it, just close Artisan using the standard Unix shortcut Ctrl + C. The very first thing to do before starting work is to install PHP with all the necessary extensions and MySQL. Detailed information about this process depends on the OS and can be found in the documentation on the profile site. The next step is to install Composer. When you have everything you need, you can install the framework in two ways - using Composer or with the Laravel installer.
Package structure
Laravel is very flexible, which is noticeable when the user uses his ability to create his own folder structure. The Laravel framework has a pre-created structure, but it can be significantly expanded. The main folder contains the following subdirectories:
- The application is the heart of the project, most of the code will be posted here.
- Console - the folder contains console commands and the kernel where they are registered.
- Events - all events are stored here.
- Exceptions are the place where exceptions are thrown. This directory also contains a file called handler.php, which is responsible for global exception handling. If you need to process them yourself, it is recommended that you create a subfolder called Handlers and fill it with files that implement the Illuminate \ Contracts \ Debug \ ExceptionHandler interface.
- HTTP is all about accessing an application from the Internet.
- Controllers - Contain the controllers used by the application.
- Middleware - includes decorator classes that can modify the request before it is sent to the controller, as well as the response before sending it to the user.
- Requests - contain files responsible for automatic verification of requests.
- Routes.php - this file contains routing.
- Jobs - this folder contains “pending jobs” that may be performed in the future.
- Listeners - event processing will take place here. It also hosts listeners and subscribers.
- Policies are the applied policies used to manage user rights.
- Suppliers - Contain suppliers. These are the classes responsible for loading and managing applications.
- Model - a folder for storing models.
- Contracts are another folder that you need to arrange. This time, it is based on the Laravel programming style, where they create interfaces called contracts, which will group the functionality of the applications.
There are many more important packages in the program that you should familiarize yourself with in the documentation on the profile site before creating the project.
Configuration setting
When the framework is installed, they install Laravel into the current folder and configure. In most cases, you can stick to the default values if you do not need to use some additional functions. The bulk of the options are well-documented, for example:
- Env. The first step in each configuration is to prepare the .env file. This is the most important file because it contains all the credentials and server-specific configuration, unlike the applications that are stored in the config directory.
- APP_ENV - This value defines the environment used on the server. It can have two different values: local, which prepares the application for testing on the development server, and production, fully optimizing the application for mass use in production.
- APP_DEBUG - This field determines whether debugging tools are enabled or not. When installing Laravel according to the recommendations of experienced developers, this field requires you to keep it in sync with APP_ENV. If APP_ENV = production, then APP_DEBUG = false, otherwise APP_DEBUG = true.
- APP_KEY is a random key used in the process of generating and verifying passwords. If the developer changes this value after the initial configuration, this will lead to the fact that all passwords created by Laravel will cease to be valid and require regeneration. This field is automatically populated during the installation of Laravel. If you need to change it, use the following command: "./artisan key: generate".
- APP_URL - it is known that this is the url address that is presented and used to create paths from links and assets.
- DB - these six fields are used to configure the database connection. Most fields are self-explanatory. The only exception is DB_CONNECTION, where developers must register mysql.
Database usage
CONFIG / database.php - the file allows you to create a more advanced database configuration. Laravel without installing any external packages supports the following solutions: MySQL, pgSQL, SQLite, SQLSRV. Most configurations are already prepared, the only exception being SQLSRV, which requires adding another section.
In this file, you can select the name of the table used to store migrations, as well as configure the Redis connection.
CONFIG / mail.php - this file is responsible for sending emails. All necessary parameters are set in the .env file. Using SMTP is not the only way to send emails to Laravel. Alternatives include the following:
- Sending the mail script along the path of the script that is installed in its field.
- Mail for using the mail () function of PHP and third-party solutions (mailgun, mandril, amazon ses or sparkpost).
During development, you can install a driver to register and save email in the log instead of sending a message or adding the following section:
'to' => [
'address' => 'dev@domain.com'
'name' => 'Dev Example']
After that, all email addresses will be automatically redirected to the target address.
File system management
CONFIG / FILESYSTEMS.PHP - this file allows you to manage the file systems that the developer uses in his project. Laravel uses many file systems thanks to a library called Flysystem. Sometimes it is necessary that several files are stored locally, and some in the cloud. This combination is also possible with Laravel. The default configuration allows you to store files with open access in storage / app and storage / app / public. Amazon Cloud (s3) is also available. Perform some changes and replace the s3 partition with the following code.
Thus, you can place the credentials in one place (.env file). This will result in cleaner code, and the keys are much safer without the risk of accidentally placing them in the repository.
CONFIG / QUEUE PHP Queuing System
Queues are a way to complete time-consuming tasks in the "later" mode. There are several solutions available for this:
- Synchronization. This is the default and the simplest solution, which means no queues. The job queue is executed while the script is running. This is the only solution that does not need additional infrastructure. You can leave it if you do not plan to use queues for short tasks.
- Database. The simplest methods that will use the database to store jobs in the queue. The configuration requires that a connection to the database and table be selected for storing tasks. In this case, the driver field must be the actual name of the connection, not the driver. The necessary migration can be generated using the "./artisan queue: table" wizard. In the case of the database engine, it is very important to set the expire field correctly. It is used to prevent the processing of the same work twice. To do this, it must be higher than the timeout value used at startup.
- Redis The configuration is as simple as choosing a connection name and expire, and beanstalk and sqs are commercial solutions that can be configured using the correct credentials. It is proposed to use the env () function to store them in the .env file.
- Failed - used to select the table in the database that is needed to store failed jobs. Migration can be created using the "./artisan queue: failed-table" wizard.
Configuring third-party packages
Some third-party packages require additional configuration. In most cases, they will use a file with a name that matches their name, but some of them do not. These packages store their configuration in CONFIG / SESSIONS.PHP. This file is responsible for setting up sessions. Lifetime and expire_on_close will indicate how the recorded session should be stored if it should be deleted after closing the browser. The driver field will determine where the session should be stored:
- file - stored in files placed in the folder selected with the file field;
- cookie - sent with each response and returned in every browser request;
- database - stored in the database, which is determined by the connection fields and tables.
Migration can be done using the wizard:
- ./artisan session: table;
- apc - stored in RAM of servers;
- memcached - is stored on the memcached server using the configuration from the cache.php file;
- redis - stored on the redis server;
- array - the session is saved for only one call. This is a technically disabled session system.
There is also a package that needs to be added before starting development. Laravel-DebugBar - a package that will add a panel at the bottom of the website, needed for debugging. This panel will only be displayed if debugging is enabled. Installing this package is very simple.
Prerequisites
Before installing Laravel, make sure that the following sections and elements are available:
- Web server.
- PHP
- MySQL
- Composer install Laravel.
- Integrated Development Environment (IDE).
For example, it will use the XAMPP cross platform. It comes with Apache, MySQL and PHP. If the user does not have XAMPP, you can download it on the Internet. If he prefers to work with other types of web servers, that is, IIS created on a web server, it is also well suited for Laravel.
Composer is a dependency manager for PHP. You can read about it on the official website and download it using the link provided.
Install Laravel on Windows 10
After installation, Composer switches to htdocs in XAMPP. Find the root web server and open the Windows Explorer application:
C: / xampp / C: / xampp / htdocs.
Next, you need to click anywhere and select Composer, as shown in the image below.
Choose a composer. The following command prompt window appears:
Create a new project using a composer. Run: composer create-project laravel / laravel larashop.
At the command prompt, you get the following result:
Wait for the installation to complete, and conduct testing.
To do this, enter localhost / larashop / public / into the browser. The question may arise: url address - what is it? Let us explain that this address opens the guest page to the user.
Setting up a new project
Application configuration information is in config / app.php. When setting up a new project, the following steps are carried out in sequence:
- Set the debugging mode, which is used to determine how much information should be displayed when an error occurs.
- Set the time zone - this parameter is used for date and PHP functions.
- Install the application key whose value is used for encryption.
The procedure for installing Laravel:
- Open the file /config/app.php. Find the following code: 'debug' => env ('APP_DEBUG', false).
- Update it to the following code: 'debug' => env ('APP_DEBUG', true). It sets the debug mode to true. This will cause Laravel to display detailed information when an error occurs. It is useful for troubleshooting.
- Set time zone. To do this, find the code 'timezone' => 'UTC', set the time zone to UTC. This is the default value. If you need to have a different time zone, you can replace UTC with the desired value.
- Set application key. To do this, find the following code 'key' => env ('APP_KEY', 'SomeRandomString').
- Update it to the code 'key' => env ('APP_KEY', 'inesindinemwanawabambuyabakoiwe'). Where it is a 32-character string that will be used as the encryption key.
Authentication Configuration
The authentication configuration file is located in "./config/auth.php". In general, the default values are left as they are. If necessary, you can change them, according to user requirements. The database configuration file is located in "./config/database.php". By default, MySQL will be used as the database engine. You can install it in another database management system, if necessary.
Update the following keys:
- Database.
- Username.
- Password.
The values in the above example:
- 'database' => env ('DB_DATABASE', 'larashop') sets the database name to larashop. You can continue and create an empty larashop database in MySQL.
- 'username' => env ('DB_USERNAME', 'root') sets the root as the database username to be used for authentication. You must use a valid username for your MySQL instance.
- 'password' => env ('DB_PASSWORD', 'melody') sets the melody as the password that will be used to log into the system. You must use a valid password for your MySQL instance.
In this way, a well-configured and fully operational application base powered by Laravel will be created. In general, experts confirm that it is very simple, intuitive and user-friendly.