Laravel is a Web application framework with an expressive, elegant syntax.
Laravel is a Web application framework with expressive, elegant syntax. Laravel is committed to providing an amazing developer experience while providing powerful features, Like thorough dependency injection, expressive database abstraction layers, queuing and scheduling jobs, unit and integration testing, and more, Laravel is a framework that can grow with you whether you’re new to PHP Web frameworks or have years of experience.
Laravel features
- Simple, fast routing engine.
- Powerful dependency injection container.
- Multiple backends for session and cache storage.
- Expressive and intuitive database ORM.
- database-independent schema migration.
- Powerful background job handling.
- Real-time event broadcast.
Laravel install
Getting started with macOS
If you’re developing on a Mac and have Docker Desktop installed, you can create a new Laravel project with a simple terminal command. For example, to create a new Laravel application in a directory named “example-app”, you can run the following command in your terminal:
curl -s "https://laravel.build/example-app" | bash
Of course, you can change the “example-app” in this URL to anything you like – just make sure the app name contains only alphanumerical characters, dashes, and underscores. The directory for your Laravel application will be created in the directory where you executed the command.
After creating the project, you can navigate to the application directory and launch Laravel Sail. Laravel Sail provides a simple command-line interface for interacting with Laravel’s default Docker configuration:
cd example-app
./vendor/bin/sail up
The first time you run the Sailup command, Sail’s application container will be built on your local computer. This may take a few minutes. Don’t worry-it will be much faster to try to start Sail later.
Start on Windows
Before we create a new Laravel application on your Windows machine, make sure to install Docker Desktop. Next, you should ensure that the Windows Subsystem for Linux 2 (WSL2) is installed and enabled. WSL allows you to run Linux binary executables locally on Windows 10.
Start a Windows terminal and start a new terminal session for your WSL2 Linux operating system. Next, you can use a simple terminal command to create a new Laravel project. For example, to create a new Laravel application in a directory named “example-app”, you can run the following command in your terminal:
Linux
If you’re developing on Linux and have Docker Compose installed, you can create a new Laravel project with a simple terminal command. For example, to create a new Laravel application in a directory named “example-app”, you can run the following command in your terminal:
curl -s https://laravel.build/example-app | bash
Of course, you can change the “example-app” in this URL to anything you like – just make sure the app name contains only alphanumerical characters, dashes, and underscores. The directory for your Laravel application will be created in the directory where you executed the command.
After creating the project, you can navigate to the application directory and launch Laravel Sail. Laravel Sail provides a simple command-line interface for interacting with Laravel’s default Docker configuration:
cd example-app
./vendor/bin/sail up
When you run the Sailup command for the first time, Sail’s application container will be built on your local computer. This may take a few minutes. Don’t worry-it will be much faster to try to start Sail later.
Laravel full-stack framework
Laravel works as a full-stack framework. A “full-stack” framework means that you’ll use Laravel to route requests to your application and render your frontend via Blade templates or single page application hybrid technologies like Inertia.js. This is the most common way to use the Laravel framework and, in our opinion, the most efficient way to use Laravel.
If this is how you plan to use Laravel, you might want to check out our documentation on routes, views, or Eloquent ORM. Also, you might be interested in learning about community packages like Livewire and Inertia.js. These packages allow you to use Laravel as a full-stack framework while enjoying many of the UI benefits offered by single-page JavaScript applications.
Laravel API backend
Laravel can also be used as an API backend for JavaScript single-page applications or mobile applications. For example, you can use Laravel as an API backend for your Next.js application. In this case, you can use Laravel to provide authentication and data storage/retrieval for your application while also taking advantage of Laravel’s powerful services such as queue, email, notifications, and more.
Laravel deployment
Server requirements
- PHP > = 8.0
- BCMath PHP extension
- Ctype PHP extension
- cURL PHP extensions
- DOM PHP extension
- File info PHP extension
- JSON PHP extension
- Mbstring PHP extension
- OpenSSL PHP extensions
- PCRE PHP extension
- PDO PHP extensions
- Tagger PHP extension
- XML PHP extension
Nginx
If you are deploying your application to a server running Nginx, you can use the following configuration file as a starting point for configuring your Web server. Most likely, you’ll need to customize this file based on your server configuration.
Make sure that, like the configuration below, your Web server directs all requests to your application’s public/index.php file. You should never attempt to move the index.php file to the root of your project because serving the application from the root of your project exposes many sensitive configuration files to the public Internet:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /srv/example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
When deploying to production, make sure you are optimizing Composer’s class autoloaders mappings so that Composer can quickly find the right file to load for a given class:
php artisan config:cache
This command combines all the Laravel configuration files into a single cache file, which drastically reduces the number of times the framework has to access the filesystem when loading configuration values.
Laravel request lifecycle
Step 1
The entry point for all requests to your Laravel application is the public/index.php file. All requests are directed to this file by your Web server (Apache/Nginx) configuration. The index.php file doesn’t contain much code. Instead, it is the starting point for loading the rest of the framework.
This index.php file loads the autoloader definition generated by Composer, and then the first action taken from bootstrap/ app.php.Laravel itself is to create an instance of the application/service container.
HTTP/console kernel
Next, the incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request entering the application. These two cores act as the central place through which all requests flow. For now, let’s focus only on the app/Http/Kernel.php.
The HTTP Kernel extends the Illuminate\Foundation\Http\Kernel class, which defines an array that bootstrappers will run before a request is executed. These bootstrapping programs configure error handling, configure logging, detect the application environment, and perform other tasks that need to be done before the request can actually be processed. Typically, these classes handle the internal Laravel configuration that you don’t need to worry about.
The method signature handle for the HTTP kernel method is very simple: it takes aRequest and returns aResponse. Think of the kernel as a big black box that represents the entire application. Give it an HTTP request and it will return an HTTP response.
Service provider
One of the most important kernel boot operations is loading a service provider for your application. All service providers for the application are configured in the providers array of the config/app.php configuration file.
Laravel will iterate through this list of providers and instantiate each of them. After the provider is instantiated, register calls the method on all providers. Then, once all providers have been registered, boot calls the method on each provider. This way the service provider may rely on each container binding to be registered and available when its boot method is executed.
The service provider is responsible for bootstrapping all the various components of the framework, such as the database, queue, validation, and routing components. Basically every major feature provided by Laravel is bootstrapping and configured by the service provider. Service providers are the most important aspect of the overall Laravel bootstrapping process due to the many features that they bootstrap and configure the framework provides.
route
One of the most important service Providers in an application is App\Providers\RouteServiceProvider. This service provider loads the routes file that is included in your application’s routes directory.
Once the application is booted and all service providers are registered, the Request is handed over to the router for scheduling. The router will dispatch requests to routes or controllers and run any router-specific middleware.
The
middleware provides a convenient mechanism to filter or inspect HTTP requests coming into an application. For example, Laravel includes a middleware for verifying that your application’s users are authenticated. If the user is not authenticated, the middleware redirects the user to the login screen. However, if the user is authenticated, the middleware will allow the request to go further into the application.
overall
Once the route or controller method returns the response, it is passed back through the route’s middleware, giving the application a chance to modify or inspect the outgoing response.
Finally, once the response is returned through the middleware, the handle method of the HTTP kernel returns the response object, and the index.php file calls the send method on the returned response. The send method sends the response content to the user’s web browser.
Database: getting started
Config
The Laravel database service configuration is located in the config/database.php configuration file of the application. In this file, you can define all database connections and specify which one should be used by default. Most of the configuration options in this file are driven by the values of the application environment variables.
By default, Laravel’s sample environment configuration is ready to be used with Laravel Sail, a Docker configuration for developing Laravel applications on a local machine. However, you are free to modify the database configuration according to the needs of your local database.
SQLite config
The SQLite database is contained in a single file on the file system. Touch you can use the command to create a new terminal, SQLite database: touch the database/database SQLite. Once the database is created, you can easily configure the environment variable to point to that database by putting the absolute path to the database in the DB_DATABASE environment variable:
DB_CONNECTION=sqlite
DB_DATABASE=/absolute/path/to/database.sqlite
To enable foreign key constraints for SQLite connections you should set the DB_FOREIGN_KEYS environment variable to true:
DB_FOREIGN_KEYS=true
Run SQL query
With the database connection configured, you can run queries using the DB facade. The facade provides methods for each type of DB query: select, update, insert, delete, and statement.
To run a basic SELECT query, you can use the method on the facade select: DB
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class UserController extends Controller
{
/**
* Show a list of all of the application's users.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = DB::select('select * from users where active = ?', [1]);
return view('user.index', ['users' => $users]);
}
}
The first argument passed to the select method is the SQL query, and the second argument is any parameter bindings that need to be bound to the query. In general, these are the values of where clause constraints. Parameter binding provides protection against SQL injection.
This select method will always return an array result. Each result in the array is a PHPstdClass object representing a record in the database:
use Illuminate\Support\Facades\DB;
$users = DB::select('select * from users');
foreach ($users as $user) {
echo $user-> name;
}
—END—
license: MIT license