Laravel 11 Framework Creation and Problems Encountered [Full Analysis]


theme: cyanosis highlight: a11y-dark

Laravel 11 further improves on the foundation of Laravel 10.x, introducing a simplified application structure, per-second rate limiting, health check routes, graceful encryption key rotation, improved queue testing, Resend mail transport, Prompt validator integration, new Artisan commands, and more. In addition, Laravel Reverb, an official scalable WebSocket server, has been introduced to provide powerful real-time functionality for your application.

Official Documentation: Installation - Laravel 11.x - The PHP Framework For Web Artisans

1. Create a Laravel Project

Before creating your first Laravel project, ensure that PHP and Composer are installed on your local machine.

1.1 Environment

PHP 8.2

Laravel 11.x requires a minimum PHP version of 8.2.

Composer

1.2 Project Creation

Before creating, check php.ini to ensure the following extensions are enabled. If they are disabled, remove the leading ; comment symbol

extension=openssl
extension=php_pdo_mysql.dll  (pdo_mysql)
extension=mbstring
extension=zip
extension_dir = "ext"
extension=pdo_pgsql
extension=curl

Run the following command to check if the openssl extension is loaded:
php -m | findstr openssl

If you do not have a `php.ini` file, rename either php.ini-production or php.ini-development to php.ini

Use the php -m command to check if the extensions have been enabled:

bcmath
ctype
json
tokenizer
xml

After setting up your environment, you can create a new Laravel project via Composer's create-project command:

composer create-project laravel/laravel:^11.0 example-app

Alternatively, you can globally install the Laravel Installer via Composer to create new Laravel projects:

composer global require laravel/installer

laravel new example-app

1.3 Install Dependencies

After creating the project, navigate to the project folder and install dependencies

cd example-app

composer install

To resolve slow download speeds, enable this extension in `php.ini`

extension=curl

Switch Composer to a domestic mirror source (for users in China)

composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

If you encounter the error Your requirements could not be resolved to an installable set of packages., this issue is caused by incorrect version configuration. First, check that your version number is formatted correctly.

Check your Composer version

composer -v

If you are not running the latest version, run the command to update Composer

composer self-update

Check your PHP version

php -v

If your versions are correct, run the install command ignoring platform requirements

composer install --ignore-platform-req=ext-fileinfo
Or
composer update --ignore-platform-req=ext-fileinfo

Laravel 11 uses SQLite as the default SQL database engine. After creating a Laravel project, an SQLite database file is automatically created in the database folder, and the database structure is built from migration files.

If you do not want to use SQLite, modify the .env file as follows:

# Modify timezone and locale
APP_TIMEZONE=PRC
APP_LOCALE=zh-CN

Modify MySQL configuration

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=123456

Please create the relevant database first.

If you encounter an error, first delete the already created users table from your database

Then add the following code to app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\Schema;
Schema::defaultStringLength(250);

Run the database migrations

php artisan migrate

1.4 Start the Server

Use Laravel Artisan's serve command to start Laravel's local development server:

php artisan serve

After the server starts, you can access your project via http://127.0.0.1:8000.

2. Common Issues

Issue 1: Cannot download dependency packages

Solution:

Manually download dependencies -> run composer install

Issue 2: Encountered the error "Your requirements could not be resolved to an installable set of packages."

Solution

This error is caused by incorrect version configuration. First, use composer -v and php -v to check that your versions are correct.

Run this command to resolve:

composer install --ignore-platform-req=ext-fileinfo

Issue 3: Modifications to .env file have no effect

This issue can also cause migration errors after deleting and clearing your database

Solution:

Clear the configuration cache

php artisan config:cache

Issue 4: Returned error message: "could not driver"

Solution:

1. Check if required extensions are enabled. Open `php.ini` and confirm the following two extensions are uncommented

extension=php_pdo_pgsql.dll
extension=php_pgsql.dll

2. Check that your connection information is correct

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
# Database port
DB_PORT=5432
# Database name
DB_DATABASE=
# Database username
DB_USERNAME=
# Database password
DB_PASSWORD=

3. Check that the required dll file exists in your integrated development environment

Issue 5: Error message "Error"

class "App\Providers\Schema" not found

Solution:

The relevant dependency is not imported in app/Providers/AppServiceProvider.php. Adding the import will resolve the issue

use Illuminate\Support\Facades\Schema;

Issue 6: NO application encryption Key has been specifled.

Solution:

Run the command to generate the encryption key

php artisan key:generate

Issue 7: Database file at path [l:.bolg_laravel_adlerian\app_laravel\database\database.sqlite] does not exist. Ensure this is an absolute path to the database.

SQLite does not have an existing database, run the migration command to create it


END | Creative Commons License, image save failed, it is recommended to upload the image file directly

This is a discussion topic separated from the original thread at https://juejin.cn/post/7369117760460324902