Tự học Laravel: Cài đặt LARAVEL

Install Laravel and create a project.

REQUIREMENTS

The following requirements must be fulfilled to operate laravel.

  • PHP >= 7.1.3
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension

INSTALL COMPOSER

laravel uses package (library) dependency management composer. You need to install composer on the computer that runs laravel.

curl -sS https://getcomposer.org/installer | php mv composer.phar /usr/local/bin/composer 

LARAVEL INSTALLATION AND PROJECT CREATION

There are two ways to create a project in laravel.

  1. Create a project via composer
  2. Create a project with the laravel installer

CREATE PROJECT VIA COMPOSER

Create a project with the composer command and the create-project option.

composer create-project --prefer-dist laravel/laravel PROJECT_NAME

The part of ‘laravel/laravel’ is ‘vendor name/package name’. Specify ‘laravel/laravel’ to create a laravel project.

The ‘-prefer-dist’ part is optional, and the following two types can be specified.

  • -Prefer-soruce: Download the development version from the source code repository.
  • -Prefer-dist: Download the release version, stable version.

Also, via composer, you can specify a version, and you can create a project with an older version of Laravel. The following is an example of specifying ver 4.2.

composer create-project laravel/laravel --prefer-dist PROJECT_NAME 4.2

CREATE PROJECT WITH LARAVEL INSTALLER

First use composer to install the laravel installer.

composer global require "laravel/installer"

When the download is complete, a ~/.composer/vendor/bin/laravel file is created. This is the laravel installer command. In order to execute the command, ~/.composer/vendor/bin it is necessary to pass through PATH.

# ~/.bashrc 
export PATH=$HOME/.composer/vendor/bin:$PATH

laravel new Create a project using commands.

laravel new PROJECT_NAME

ENVIRONMENTAL SETTING

Change to the project directory and set the environment.

cd PROJECT_NAME

.ENV

If the .env file has not been created, copy it from .env.example and create it php artisan key:generate.

cp .env.example .env
php artisan key:generate

The. env file contains information that needs to be separated for each execution environment. Passwords and database names can be set individually for each development machine, staging machine, commercial machine, etc.

php artisan key:generate Will set the application-specific random string to APP_KEY in .env. APP_KEY is required to secure user session information and password encryption etc.

  • If you are using git, don’t forget to add .env to your .gitignore file!

CONFIG/APP.PHP

Set the time and language.

// config/app.php
return [
    // ... 
    'timezone' => 'Asia/Ho_Chi_Minh',
    'locale' => 'en', 
    // ...
];

CONFIG/DATABASE.PHP

Set up the database. 
The following example is an example of setting DB to sqlite.

.envDB_CONNECTION=sqliteAdd to file

# .env 
DB_CONNECTION=sqlite
# sqlite
# DB_DATABASE=homestead

DB_CONNECTIONBy sqlitespecifying , Laravel uses the DB connection settings config/database.phpdefined in it sqlite.

// config/database.php 
return [
    ... 
    'default' => env('DB_CONNECTION', 'mysql'), 
    ... 
    'connections' => [
        'sqlite' => [
            'driver' => 'sqlite',
            'database' => env('DB_DATABASE', database_path('database.sqlite')),
            'prefix' => '',
        ],
        ...
    ],
    ...
];

In the above sqlite connection settings, if the .env does not have a DB_DATABSE setting, it is set to use the database/database.sqlite file, so create that file.

touch database/database.sqlite

PERMISSIONS

You need read and write privileges in the strage and bootstrap/cache directories. According to the www server settings, chown, chmode and set.

# chown example chown -R USER_NAME:GROUP_NAME storage 
chown USER_NAME:GROUP_NAME bootstrap/cache
  • Please replace USER_NAME and GROUP_NAME according to the setting of www server.

EXECUTION

Run the Laravel app on PHP’s built-in server.

php -S localhost:8000 -t public

-tOptionally publicspecify a directory that is the document root of the Laravel project .

If you access localhost:8000 in your www browser , Laraveland a large logo is displayed, you have successfully installed.

Built-in servers artisancan also be executed by commands. It may be shorter and easier to use.

php artisan serve

Press Ctrl-C to exit the built-in server.

php artisan serve

Laravel development server started: <http://127.0.0.1:8000>
[Mon Jul 28 18:36:42 2018] 127.0.0.1:60702 [200]; /favicon.ico
[Mon Jul 28 18:36:44 2018] 127.0.0.1:60706 [200]; /favicon.ico

^ C

NEXT STEP

I succeeded in creating the first project of Laravel. What should I start from now on to learn application development with Laravel? The following Laracast is recommended as a first step. It is English, but I think you can understand it. However, this video is the contents of v5.4.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.