Laravel 4 Introductory Tutorial Installation and introduction _php example

Source: Internet
Author: User
Tags generator php script php website

0. Default condition

This article by default you already have a perfect php+mysql running environment, know the basic knowledge of PHP website operation. After you follow this tutorial, you will get a simple blogging system that contains logins, and 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+

1. Installation

Many people were stopped at the first step of learning Laravel, installed. This is not because of the complexity of the tutorial, but because of "known reasons". Here I recommend a composer full volume Chinese mirror image: http://pkg.phpcomposer.com/. The "Modify Composer Profile" method is recommended for configuration. I am writing this tutorial with this mirror test, installation failed, if you also appear this situation, you can try another composer China Mirror: http://composer-proxy.com/.

Once the mirror configuration is complete, switch to the directory where you want to place the site and run the command:

Copy Code code as follows:

Composer Create-project Laravel/laravel Learnlaravel

Then, wait a moment, the current directory will appear a folder called Learnlaravel, this time if you access the learnlaravel/public/directory through the browser, the basic will display Error in exception handler. , this is because the Learnlaravel/app/storage directory does not have 777 permissions, set the right to see the page as shown below:

Congratulations on your ~laravel installation success!

Do not want to configure the image of the classmate, you can use the Laravel sector is very famous super get install artifact: Https://github.com/overtrue/latest-laravel

2. Necessary plug-in installation and configuration

We use the famous Sentry plug-in to build a login authorization system.

Open./composer.json, change to:

Copy Code code as follows:

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

Then, run the command under the project root directory

Copy Code code as follows:

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:

Copy Code code as follows:

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

Put it under "require".

Run composer Update, and then add the configuration in./app/config/app.php:

Copy Code code as follows:

' Way\generators\generatorsserviceprovider '

With the installation complete, running PHP artisan on the command line, you can see many of the 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:

Copy Code code as follows:

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

Copy Code code as follows:

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, I would roughly say:

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

Copy Code code as follows:

' Cartalyst\sentry\sentryserviceprovider ',
' Sentry ' => ' cartalyst\sentry\facades\laravel\sentry ',

The database configuration for the permissions 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:

Copy Code code as follows:

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:

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

Modify in ***_create_pages_table.php:

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

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

Copy Code code as follows:

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:

Copy Code code as follows:

PHP Artisan Generate:model Article
PHP Artisan Generate:model Page

At this time, under the./app/models/, there are two model files. These two classes inherit the core class \eloquent provided by Laravel.

5. Database padding

Run the following commands individually:

Copy Code code as follows:

PHP Artisan generate:seed Page
PHP Artisan generate:seed Article

At this point, there are two new files in the./app/database/seeds/, 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:

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

Copy Code code as follows:

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

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

Copy Code code as follows:

PHP Artisan Db:seed

After the operation is completed go to the database to see, the data has been filled in, article and page each 10 lines.

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.