Creating and Running a Laravel Project
What is Laravel?
Laravel is an open-source PHP web framework created by PHP developer Taylor Otwell. It aims to provide developers with a clean and elegant toolkit for building modern web applications. Laravel offers a rich set of features, such as routing, session, caching, authentication, etc., that simplify common web development tasks.
Main Features and Advantages of Laravel
- Simplicity and Elegance: Laravel's syntax is simple and elegant, making the code more readable and maintainable.
- Rich Feature Set: Includes routing, authentication, session management, and caching, significantly simplifying the development process.
- Powerful ORM: Eloquent ORM provides a simple ActiveRecord implementation for database operations.
- Modular Design: Easily integrate third-party libraries and extensions through the Composer package manager.
- Strong Community Support: Large developer community and rich ecosystem.
Laravel helps developers quickly build high-quality web applications and APIs, suitable for projects of all sizes, from small applications to large enterprise-level systems.
Creating and Running a Laravel Project with ServBay
In this article, we will use ServBay's PHP environment to create and run a Laravel project. We will utilize ServBay's 'Host' feature to set up the web server and access the project through simple configuration.
Note: If you were previously an NGINX or Apache user
ServBay uses Caddy as the default web server. There are some key changes to note for users transitioning from NGINX and Apache to ServBay:
Caddy Configuration
ServBay has Caddy built-in, and it is already optimized and debugged by default. Developers only need to use ServBay's 'Host' feature to manage the site, without manually modifying the Caddy configuration files.
Rewrite Rules and .htaccess
In NGINX and Apache, developers usually need to write their own rewrite rules and .htaccess files to handle URL rewriting and other configurations. However, ServBay's default settings already configure Caddy's rules, so unless there are special requirements, developers do not need to write these rules themselves.
Learn More
For more information, please refer to Rewrite and htaccess, How to Migrate Apache Sites to ServBay, How to Migrate NGINX Sites to ServBay.
Creating a Laravel Project
TIP
ServBay recommends developers place the website in the /Applications/ServBay/www
directory for easy management.
Install Composer
ServBay comes with Composer pre-installed, no need for separate installation.
Create a Laravel Project
Use Composer to create a new Laravel project:
bashcd /Applications/ServBay/www mkdir servbay-laravel-app cd servbay-laravel-app composer create-project --prefer-dist laravel/laravel .
1
2
3
4Enter the Project Directory
Navigate to the newly created Laravel project directory:
bashcd /Applications/ServBay/www/servbay-laravel-app
1
Initial Configuration
Generate Application Key
Laravel requires an application key for ensuring the security of user sessions and other encrypted data. You can generate an application key using the Artisan command:
bashphp artisan key:generate
1Configure Environment Variables
Configure the database connection information and other environment variables in the
.env
file. Ensure the following configurations are correctly set:APP_NAME=Laravel APP_ENV=local APP_KEY=base64:... APP_DEBUG=true APP_URL=https://servbay-laravel-test.local LOG_CHANNEL=stack DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Configuring the Web Server
Use ServBay's 'Host' feature to access the Laravel project through the web server. In ServBay's 'Host' settings, add a new host:
- Name:
My First Laravel Dev Site
- Domain:
servbay-laravel-test.local
- Website Type:
PHP
- PHP Version: Choose
8.3
- Website Root Directory:
/Applications/ServBay/www/servbay-laravel-app/public
For detailed steps, please refer to Adding the First Website.
Adding Sample Code
Add the following code to the routes/web.php
file to output "Hello ServBay!":
Route::get('/', function () {
return 'Hello ServBay!';
});
2
3
Accessing the Website
Open a browser and visit https://servbay-laravel-test.local
, you will see the page output Hello ServBay!
.
NoSQL Database Example
Memcached Example
Install Memcached Extension
In ServBay, the Memcached extension is pre-installed, no additional installation is needed.
Configure Memcached
Configure Memcached connection information in the
.env
file:CACHE_STORE=memcached MEMCACHED_HOST=127.0.0.1
1
2Use Memcached
Use caching in the routes
routes/web.php
:phpuse Illuminate\Support\Facades\Cache; Route::get('/memcached', function () { Cache::put('key', 'value', 600); return Cache::get('key'); });
1
2
3
4
5
6Open a browser and visit
https://servbay-laravel-test.local/memcached
Redis Example
Install Redis Extension
In ServBay, the Redis extension is pre-installed, no additional installation is needed.
Configure Redis
Configure Redis connection information in the
.env
file:CACHE_STORE=redis REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379
1
2
3
4Use Redis
Use caching in the routes
routes/web.php
:phpuse Illuminate\Support\Facades\Redis; Route::get('/redis', function () { Redis::set('key', 'value'); return Redis::get('key'); });
1
2
3
4
5
6Open a browser and visit
https://servbay-laravel-test.local/redis
Relational Database Example
Creating Database Structure and Migration Files
MySQL Example
Configure MySQL
Configure MySQL connection information in the
.env
file:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6Create Migration File
Use the Artisan command to create a migration file:
bashphp artisan make:migration create_accounts_table
1Edit Migration File
Find the newly created migration file in the
database/migrations
directory and edit it to define the database table structure:phppublic function up() { Schema::create('accounts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
1
2
3
4
5
6
7
8
9Run Migration
Use the Artisan command to run the migration to create the database table:
bashphp artisan migrate
1Insert Account Data
Insert account data in the routes
routes/web.php
:phpuse Illuminate\Support\Facades\DB; Route::get('/mysql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Use MySQL
Query the database in the routes
routes/web.php
:phpuse Illuminate\Support\Facades\DB; Route::get('/mysql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6Open a browser and visit
https://servbay-laravel-test.local/mysql-add
andhttps://servbay-laravel-test.local/mysql
PostgreSQL Example
Configure PostgreSQL
Configure PostgreSQL connection information in the
.env
file:DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=servbay_laravel_app DB_USERNAME=root DB_PASSWORD=password
1
2
3
4
5
6Create Migration File
Use the Artisan command to create a migration file:
bashphp artisan make:migration create_accounts_table
1Edit Migration File
Find the newly created migration file in the
database/migrations
directory and edit it to define the database table structure:phppublic function up() { Schema::create('accounts', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); }
1
2
3
4
5
6
7
8
9Run Migration
Use the Artisan command to run the migration to create the database table:
bashphp artisan migrate
1Insert Account Data
Insert account data in the routes
routes/web.app
:phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql-add', function () { DB::table('accounts')->insert([ 'name' => 'ServBay', 'email' => '[email protected]', ]); return 'Account added'; });
1
2
3
4
5
6
7
8
9Use PostgreSQL
Query the database in the routes
routes/web.php
:phpuse Illuminate\Support\Facades\DB; Route::get('/pgsql', function () { $users = DB::table('accounts')->get(); return $users; });
1
2
3
4
5
6Open a browser and visit
https://servbay-laravel-test.local/pgsql-add
andhttps://servbay-laravel-test.local/pgsql
By following the above steps, you have successfully created and run a Laravel project, managed and accessed your project using ServBay's features, connected to multiple databases, and queried data.