Laravel4 installation and getting started

Source: Internet
Author: User
This article does not recommend learning PHP and MVC programming. This document is not a "step by step" tutorial. This article requires you to make some mental efforts to solve some hidden tasks, big or small, to truly understand Laravel's operating logic. 0. default conditions

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

Software version: PHP 5.4 +, MySQL 5.1 +

1. install

Many people are blocked from installing Laravel in the first step. It is not because of the complexity of the installation tutorial, but because of [well-known reasons ]. Here I recommend a full-scale Chinese image of composer: http://pkg.phpcomposer.com /. We recommend that you modify the composer configuration file. I used this image for testing when writing this tutorial. the installation failed. if this happens, try another composer Chinese image: http://composer-proxy.com /.

After the image is configured, switch to the directory where you want to place the website and run the following command:

The code is as follows:


Composer create-project laravel/laravel learnlaravel

Then, wait a moment and a folder named learnlaravel will appear in the current directory. if you access the learnlaravel/public/directory through a browser, the Error in exception handler will be displayed ., this is because the learnlaravel/app/storage directory does not have the 777 permission. after setting the permission, you can see the page as follows:

Congratulations ~ Laravel is successfully installed!

Do not want to configure the image of the students, you can use the Laravel industry is very famous ultra installation artifacts: https://github.com/overtrue/latest-laravel

2. install and configure necessary plug-ins

We use the famous Sentry plug-in to build a logon and other permission verification system.

Open./composer. json and change it:

The code is as follows:


"Require ":{
"Laravel/framework": "4. 2 .*",
"Cartalyst/sentry": "2.1.4"
},

Then, run the command in the project root directory.

The code is as follows:


Composer update

Wait a moment and it will prompt that the installation of cartalyst/sentry 2.1.4 is complete.

Similarly, we will install a very powerful plug-in for development, way/generators, which is its name in the composer Library. Add the following content to composer. json:

The code is as follows:


"Require-dev ":{
"Way/generators ":"~ 2.0"
},

Put it under "require.

Run composer update and add the configuration in./app/config/app. php:

The code is as follows:


'Way \ Generators \ GeneratorsServiceProvider'

After the installation is complete, run php artisan in the command line to view many new functions provided by this plug-in.

Some people may ask why domestic images are still so slow? In fact, the slowest part of composer during update is not to download, but to download the previous dependency analysis. because Laravel relies on a large number of composer packages, PHP scripts are executed slowly, therefore, it is normal to wait two or three minutes for each update.

3. database creation and migration

The database configuration file is located in./app/config/database. php. we need to change the "mysql" item in "connections" to the configuration we need. The following is my configuration:

The code is as follows:


'Mysql' => array (
'Driver '=> 'mysql ',
'Host' => 'localhost ',
'Database' => 'laravel ',
'Username' => 'root ',
'Password' => 'password ',
'Charset' => 'utf8 ',
'Colation' => 'utf8 _ unicode_ci ',
'Prefix' => 'l4 _',
),

The prefix is the table prefix. this Laravel will be automatically maintained and you don't have to worry about writing it.

In this case, you need to go to the database to create the database, and then enter:

The code is as follows:


Php artisan migrate -- package = cartalyst/sentry

After the execution is complete, five tables are in your database, which is created by sentry. For details about how to configure sentry in Laravel4, refer to https://cartalyst.com/manual/sentry?laravel-4,4:

Add the following two lines to the corresponding position in./app/config/app. php:

The code is as follows:


'Cartalyst \ Sentry \ SentryServiceProvider ',
'Sentry' => 'cartalyst \ Sentry \ Facades \ Laravel \ Sentry ',

The database configuration of the permission system ends here.

In our simple blog system, there will be two elements, Article and Page. next we will create the articles and pages data tables and run the command line:

The code is as follows:


Php artisan migrate: make create_articles_table -- create = articles
Php artisan migrate: make create_pages_table -- create = pages

At this time, go. /app/database/migrations, two more files are displayed. this is the database migration file, after a while, we will operate artisan to convert the two tables described in the two files into the real two tables in the database. rest assured that everything is automatic.

Modify the following in *** _ create_articles_table.php:

The code is as follows:


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 ();
});

In *** _ create_pages_table.php, modify:

The code is as follows:


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 ();
});

The following is the time to witness the miracle and run it in the command line:

The code is as follows:


Php artisan migrate

At this time, the articles table and pages table in the database are created.

4. model Models

Next we will be connected to Laravel's most powerful part, Eloquent ORM, where we can really improve productivity. in the words of Cook, Goose Girl Ying!

Run the following statement on the command line to create two models:

The code is as follows:


Php artisan generate: model article
Php artisan generate: model page

At this time, two model files appear under./app/models. These two classes inherit from the core class \ Eloquent provided by Laravel.

5. database filling

Run the following commands:

The code is as follows:


Php artisan generate: seed page
Php artisan generate: seed article

At this time, two new files appear under./app/database/seeds/. this is our database filling file. Laravel provides automatic database filling, which is very convenient.

Generator uses Faker \ Factory as the random data generator by default, so we need to install this composer package, the address is a https://packagist.org/packages/fzaninotto/faker, installed in require-dev with generator. Complete the installation by yourself. refer to Sentry and Generator. this is the first exercise.

Next, modify the two files respectively:

The code is as follows:


Article: create ([
'Title' => $ faker-> sentence ($ nbWords = 6 ),
'Slug' => 'first-post ',
'Body' => $ faker-> paragraph ($ nbSentences = 5 ),
'User _ id' => 1,
]);
Page: create ([
'Title' => $ faker-> sentence ($ nbWords = 6 ),
'Slug' => 'first-page ',
'Body' => $ faker-> paragraph ($ nbSentences = 5 ),
'User _ id' => 1,
]);

Then, we need to add two lines in DatabaseSeeder. php so that Laravel will include the two newly added seed files when seed.

The code is as follows:


$ This-> call ('articletablesecret ');
$ This-> call ('pagetableseeder ');

We need to fill the data in the database:

The code is as follows:


Php artisan db: seed

After the operation is complete, go to the database and check that the data has been filled in. There are 10 rows in each article and page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.