Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install and use the Laravel 5 framework

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to install and use Laravel 5 framework". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to install and use Laravel 5 framework".

Laravel 5 Chinese documentation:

1. Http://laravel-china.org/docs/5.0

2. Http://www.golaravel.com/laravel/docs/5.0/

Default condition

By default in this article, you already have a well-configured PHP + MySQL running environment and understand the basics of running PHP websites. After following this tutorial, you will get a basic simple blog system that includes login, and you will learn how to use some powerful Laravel plug-ins and composer packages (Laravel plug-ins are also composer packages).

Software version: PHP 5.4, MySQL 5.1 +

This article is not recommended for people who do not understand PHP and MVC programming at all. This article is not a "follow me step by step" tutorial. This article requires you to pay a certain amount of mental effort to solve some hidden tasks, big or small, in order to truly understand the running logic of Laravel.

1. Installation

Many people are stopped in the first step of learning Laravel, installation. Not because of the complexity of installing tutorials, but because of [well-known reasons]. Here I recommend a full composer Chinese image: http://pkg.phpcomposer.com/. It is recommended to configure in the way of "modify the configuration file of composer".

After the image configuration is complete, change to the directory where you want to place the website (such as C:\\ wwwroot, / Library/WebServer/Documents/, / var/www/html, / etc/nginx/html, etc.), and run the command:

Composer create-project laravel/laravel learnlaravel5

Then, wait a moment, and a folder called learnlaravel5 will appear in the current directory.

Then configure the site root to learnlaravel5/public.

If you do not know how to configure, it is recommended to learn to configure, there are a lot of information online. If you give up, configure line 29 'url' = >' http://localhost', as your subdirectory address, and note that it should be configured all the way to * / learnlaravel5/public.

Use the browser to access the address you configured, and you will see the following screen (the address I configured locally is http://fuck.io:88):

two。 Experience Auth system and complete installation

After the above process, the installation of Laravel 5 was successful?

-- No o (╯□╰) o

View the code of the routing file `learnlaravel5/app/Http/ routes.php`:

Route::get ('/', 'WelcomeController@index'); Route::get (' home', 'HomeController@index'); Route::controllers ([' auth' = > 'Auth\ AuthController',' password' = > 'Auth\ PasswordController',])

Follow the clues in the code and let us visit http://fuck.io:88/home (please change the domain name yourself), only to jump to the landing page?

Yes, Laravel comes with an out-of-the-box Auth system, and even the page has been written.

Let's enter our email and password at will, click to log in, and you will probably get the following screen (under Mac or Linux):

Why is it blank? Check with the developer tool, the status code of this request is 500, why?

Because the `storage` directory does not have 777 permissions.

Execute the shell command:

Cd learnlaravel5sudo chmod-R 777 storage

Revisit http://fuck.io:88/home and feel free to enter your mailbox and password if you get the following picture:

Then congratulations on the successful installation of Laravel 5!

Students who do not want to configure the image can use the installation artifact of an Zhengchao, which is very famous in the Laravel world: https://github.com/overtrue/latest-laravel

3. Database establishment and migration

Laravel 5 changes the database configuration to `learnlaravel5/ .env`. Open this file, edit the following four items, and change it to the correct information:

DB_HOST=localhostDB_DATABASE=laravel5DB_USERNAME=rootDB_PASSWORD=password

It is recommended to create a new database called laravel5. For the convenience of learning, it is recommended to use a root account to operate directly.

Laravel has prepared the migration of the Auth part for us. Run the following command to perform the database migration operation:

Php artisan migrate

The results are as follows:

If you run the command and report an error, please check the database connection settings.

Now that the database migration is complete, you can open http://fuck.io:88/home and happily try to register and log in.

4. Model Models

Next we'll touch on the most powerful part of Laravel, Eloquent ORM, where productivity is really improved, to borrow a quote from Cook: goose Meizi Ying!

Run the command:

Php artisan make:model Articlephp artisan make:model Page

In the Laravel 4 era, we used the Generator plug-in to create a new Model. Now, Laravel 5 has integrated Generator into Artisan.

Now, Artisan has created two files, `Article.php` and `Page.php`, under `Article.php`. These are two Model classes. Both of them inherit the Model class `Article.php` provided by Laravel Eloquent, and both are under the namespace `\ app`. It needs to be emphasized here that creating files on the command line is no different from creating files manually, and you can also try to create these two Model classes yourself.

Model is the M in MVC, translated into a model, and is responsible for interacting with the database. In Eloquent, each table in the database corresponds to a Model class (or, of course, multiple tables).

If you transfer from other frameworks, you may not be able to adapt to the Model part mentioned here, there is no way, because Eloquent is really too powerful, there is really nothing to do, inheriting the Eloquent class can achieve a lot of functions.

If you want to learn more about Eloquent, you can read the series: Eloquent relationships in Laravel 5 Framework Learning

Next, migrate the database of articles table and pages table corresponding to Article and Page classes, and enter the `learnlaravel5/database/ migrations` folder.

Modify the following in * * _ create_articles_table.php:

Schema::create ('articles', function (Blueprint $table) {$table- > increments (' id'); $table- > string ('title'); $table- > string (' slug')-> nullable (); $table- > text ('body')-> nullable (); $table- > string (' image')-> nullable (); $table- > integer ('user_id'); $table- > timestamps ();})

Modify the following in * * _ create_pages_table.php:

Schema::create ('pages', function (Blueprint $table) {$table- > increments (' id'); $table- > string ('title'); $table- > string (' slug')-> nullable (); $table- > text ('body')-> nullable (); $table- > integer (' user_id'); $table- > timestamps ();})

Then execute the command:

Php artisan migrate

After success, tables table and pages table have appeared in the database. Go and have a look.

5. Database populated with Seeder

Create a new file `PageTableSeeder.php` under `PageTableSeeder.php`, as follows:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report