Build PHP laravel Framework Tutorial Detailed

Source: Internet
Author: User
Tags generator php and php json php script composer install

I. Installation of Composer

The Laravel framework manages code dependencies using Composer (PHP package management tool, reference Composer Chinese document). First, you need to download the Composer PHAR packaging file (composer.phar), download it in the project directory, or place it in the Usr/local/bin directory for global invocation in the system. In the Windows operating system, you can use the Composer Windows installation tool.

Second, install Laravel

Method One: Install via Laravel installer

First, download the Laravel installer via Composer.
Composer global require "laravel/installer=~1.1"
Be sure to add the ~/.composer/vendor/bin path to the PATH environment variable so that the Laravel executable can be found by the command line, and you can use the Laravel command directly at the command line later.

After the installation is successful, you can use the command laravel new to create a newly installed laravel in the directory you specify. For example, the Laravel new blog will create a directory called blogs in the current directory, which holds the newly installed laravel and the toolkit on which it relies. This installation method is much faster than the installation via Composer.

Method Two: Install Laravel via Composer's Create-project command

You can also install Laravel by executing the Composer create-project command at the command line:
Composer Create-project Laravel/laravel--prefer-dist

Method Three: Install by downloading Laravel package

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

When the Laravel framework is installed, you can update the framework using command line commands for PHP composer.phar update.

Third, the requirements of the server environment

The Laravel framework has the following requirements for the system environment:
PHP >= 5.4
MCrypt PHP Extension
Starting with the PHP 5.5 version, installation packages for some operating systems require you to manually install the PHP JSON extension module. If you are using Ubuntu, you can install it directly via the Apt-get install Php5-json command.

Iv. Display

Necessary plug-in installation and configuration
We use the famous Sentry plug-in to build a login authorization system.

Open./composer.json, change to:


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


Then, run the command under the project root directory


Composer Update


Then wait a moment, it will prompt cartalyst/sentry 2.1.4 installation complete.


Similarly, we will install a very powerful plugin for development, Way/generators, which is its name in the composer library. Increase in Composer.json:


"Require-dev": {
"Way/generators": "~2.0"
and "require" the same level, placed below, not inside oh ~.


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


The ' Way\generators\generatorsserviceprovider ' installation is complete, running PHP artisan on the command line, and you can see the many new features that this plugin brings.


Some people ask, why is it so slow to use a domestic mirror? In fact, composer in the update when the slowest place is not download, but the dependency analysis before downloading, because Laravel rely on the composer package very much, PHP script execution speed and relatively slow, so each update, such as two or three minutes is normal, the habit is good.


3. Database establishment 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. Here's my configuration:


' MySQL ' => array (
' Driver ' => ' MySQL ',
' Host ' => ' localhost ',
' Database ' => ' Laravel ',
' username ' => ' root ',
' Password ' => ' password ',
' CharSet ' => ' UTF8 ',
' Collation ' => ' utf8_unicode_ci ',
' prefix ' => ' l4_ ',
),


Prefix is the table prefix, this laravel will help us to automatically maintain, bold write don't worry.


At this point you need to go to the database to set up this database and then enter at the command line:


PHP Artisan Migrate--package=cartalyst/sentry


After the execution, you have 5 tables in your database, which sentry built on your own. Sentry configuration details under Laravel4 see https://cartalyst.com/manual/sentry#laravel-4, I would roughly say:


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


' Cartalyst\sentry\sentryserviceprovider ', ' Sentry ' => ' cartalyst\sentry\facades\laravel\sentry ', the database configuration of the permission system ends here.


Our simple blog system will have two elements, article and page, below we will create the articles and pages datasheet, command line to run:

PHP Artisan migrate:make create_articles_table--create=articles
PHP Artisan migrate:make create_pages_table--create=pages


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

In ***_create_articles_table.php, modify the following:


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


Here's the moment to witness miracles, run at the command line:


PHP Artisan Migrate


The articles and pages tables in the database are now complete.


4. Model Models
Next we will touch the most powerful part of Laravel, eloquent ORM, where the real productivity is raised, borrow Cook's words and say, goose sister English!

We run the following statements at the command line to create two model:


PHP Artisan Generate:model Article
PHP Artisan Generate:model Page At this time, there are two files article.php and page.php under app/models/, which are two model classes, and they all inherit the core classes that Laravel provides \eloqu Ent. Here you need to emphasize that the command line to create a file, and you manually create a file without any distinction, you can also try to create these two Model class Oh.

Model is the M in MVC, translated into models, and is responsible for interacting with the database. In eloquent, each table in the database corresponds to a Model class.

If you turn around from other frames, you may have a bit of a belt over the Model section is very uncomfortable, there is no way, because the eloquent is too powerful, really nothing good to do, inherit eloquent class can achieve a lot of many functions. See Eloquent Series Tutorials: deep understanding of laravel eloquent (i)--basic concepts and usage

5. Database padding

Run the following commands individually:


PHP Artisan generate:seed Page
PHP Artisan generate:seed article at this time, under./app/database/seeds/There are two new files, which is our database populate file. Laravel provides automatic database filling, which is very convenient.

Generator defaults to using Faker\factory as a random data generator, so we need to install this composer package, the address is https://packagist.org/packages/fzaninotto/faker, With generator installed in the Require-dev can be. Specific installation please complete, you can refer to Sentry and generator, this is the first practice.


Next, change the two files individually:


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 to the databaseseeder.php so that Laravel will take our new two seed files with seed.


$this->call (' Articletableseeder ');
$this->call (' Pagetableseeder ');


The next step is to actually populate the database with the data:


PHP Artisan db:seed operation after the completion of the database to see the data has been filled in, article and page each 10 lines.

Related Article

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.