PHP source code

Source: Internet
Author: User
Tags composer install
We will not use the Laravel framework much, but if you need to use it together, let's look at a detailed tutorial on building the phpLaravel framework. The specific operation details are as follows. I hope you can help. We don't need to use the Laravel framework much, but if we need to use it together, let's take a look at the detailed tutorial on setting up the php Laravel framework. The specific operation details are as follows, I hope to help you.

Script ec (2); script

1. Install Composer

The Laravel framework uses Composer (PHP package management tool, refer to the Composer Chinese document) to manage code dependencies. First, you need to download the Composer PHAR package file (composer. phar), put it in the project directory or in the usr/local/bin directory for global calls in the system. In Windows, you can use Composer's Windows Installation tool.

Ii. Install Laravel

Method 1: Use the Laravel Installer

First, download the Laravel installer through Composer.
Composer global require "laravel/installer = ~ 1.1"
Make sure that ~ /. Add the composer/vendor/bin path to the path environment variable so that the laravel executable file can be found by the command line. Then, you can directly use the laravel command under the command line.

After the installation is successful, you can use the command laravel new to create a newly installed Laravel under the directory you specified. For example, laravel new blog will create a directory named blog in the current directory, which contains the newly installed Laravel and its dependent toolkit. This installation method is much faster than using Composer.

Method 2: Use the create-project command of Composer to install Laravel

You can also run the create-project command of Composer on the command line to install Laravel:
Composer create-project laravel/laravel -- prefer-dist

Method 3: Install Laravel by downloading the Laravel package

After the Composer is installed, download the latest Laravel framework and decompress it to a directory on your server. Then, run the command line command php composer. phar install (or composer install) in the root directory of the Laravel application to install all the framework dependency packages. In this process, you need to install Git on the server to complete the installation.

After the Laravel framework is installed, you can use the command line command php composer. phar update to update the Framework.

Iii. Requirements on the server environment

The Laravel framework has the following requirements on the system environment:
PHP >=5.4
MCrypt PHP Extension
Starting with PHP 5.5, You need to manually install the JSON extension module of PHP for some operating system installation packages. If you are using Ubuntu, run the apt-get install php5-json command to install it directly.

Iv. Presentation

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:


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


Then, run the command in the project root directory.


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:


"Require-dev ":{
"Way/generators ":"~ 2.0"
}, Similar to "require", put it below, not inside ~.


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


The installation of 'way \ Generators \ generatorsserviceprovider' has been completed. run php artisan in the command line to see many new functions of 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:


'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:


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:


'Cartalyst \ Sentry \ sentryserviceprovider', 'sentry' => 'cartalyst \ Sentry \ Facades \ Laravel \ Sentry '. The permission system's database configuration 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:

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:


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:


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:


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:


Php artisan generate: model article
Php artisan generate: model page. At this time, there are two files under app/models/: Article. php and Page. php, which is two Model classes, both inherit from the core class \ Eloquent provided by Laravel. It should be emphasized that creating a file using the command line method is no different from creating a file manually. You can also try to create these two Model classes by yourself.

Model is M in MVC. It is translated as a Model and is responsible for interacting with the database. In Eloquent, each table in the database corresponds to a Model class.

If you switch from other frameworks, you may not be able to adapt to the Model section mentioned here. It's because Eloquent is so powerful that it really has nothing to do with it, inherit the Eloquent class to implement many functions. For details, see the Eloquent series Tutorials: Laravel Eloquent (1)-basic concepts and usage

5. Database Filling

Run the following commands:


Php artisan generate: seed page
In php artisan generate: seed article, two new files are displayed under./app/database/seeds/, which 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:


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.


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


We need to fill the data in the database:


Php artisan db: After the seed operation is completed, check the database. The data has been filled in. There are 10 rows for 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.