Home >Backend Development >PHP Tutorial >Laravel 4 basic tutorial installation and getting started, laravel basic tutorial_PHP tutorial

Laravel 4 basic tutorial installation and getting started, laravel basic tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:47778browse

Laravel 4 basic tutorial installation and getting started, laravel basic tutorial

0. Default condition

This article assumes that you already have a fully configured PHP+MySQL operating environment and understand the basic knowledge of PHP website operation. By following this tutorial, you will get a basic simple blog system including 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+

1. Installation

Many people are blocked from the first step of learning Laravel, installation. Not because the installation tutorial is complicated, but because of [well-known reasons]. Here I recommend a full Chinese image of composer: http://pkg.phpcomposer.com/. It is recommended to configure by "modifying the composer configuration file". I used this image to test when writing this tutorial, and the installation failed. If this happens to you, you can try another composer Chinese image: http://composer-proxy.com/.

After the mirror configuration is completed, switch to the directory where you want to place the website and run the command:

Copy code The code is as follows:

composer create-project laravel/laravel learnlaravel

Then, wait for a while, a folder called learnlaravel will appear in the current directory. At this time, if you access the learnlaravel/public/ directory through the browser, Error in exception handler. will basically be displayed. This is because learnlaravel/ The app/storage directory does not have 777 permissions. After setting the permissions, you will see the page as shown below:

Congratulations~ Laravel is successfully installed!

Students who don’t want to configure the mirror can use the very famous Chaochao installation artifact in the Laravel world: https://github.com/overtrue/latest-laravel

2. Necessary plug-in installation and configuration

We use the famous Sentry plug-in to build login and other permission verification systems.

Open ./composer.json and change it to:

Copy code The code is as follows:

"require": {
"laravel/framework": "4.2.*",
"cartalyst/sentry": "2.1.4"
},

Then, run the command in the project root directory

Copy code The code is as follows:

composer update

Then wait for a while, it will prompt that cartalyst/sentry 2.1.4 installation is complete.

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

to composer.json

Copy code The code is as follows:

"require-dev": {
"way/generators": "~2.0"
},

Place it below “require”.

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

Copy code The code is as follows:

'WayGeneratorsGeneratorsServiceProvider'

After the installation is completed, run php artisan in the command line, and you can see many new functions brought by this plug-in.

Some people may ask, why is it so slow even after using domestic mirroring? In fact, the slowest part of composer when updating is not the download, but the dependency analysis before downloading. Since Laravel relies on a lot of composer packages, and the execution speed of PHP scripts is relatively slow, it takes two or three times to wait for each update. Minutes are normal, just get used to it.

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 is my configuration:

Copy code The code is 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. Laravel will automatically maintain it for us, so don’t worry if you write it boldly.

At this time you need to go to the database to create this database, and then enter in the command line:

Copy code The code is as follows:

php artisan migrate --package=cartalyst/sentry

After the execution is completed, there will be 5 tables in your database, which were created by sentry itself. For details on the configuration of sentry under Laravel4, please see https://cartalyst.com/manual/sentry#laravel-4. Let me give you a rough overview:

Add the following two lines at the corresponding locations in ./app/config/app.php:

Copy code The code is as follows:

'CartalystSentrySentryServiceProvider',
'Sentry' => 'CartalystSentryFacadesLaravelSentry',

The database configuration of the permission system ends here.

Our simple blog system will have two elements, Article and Page. Next we will create articles and pages data tables and run them from the command line:

Copy code 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 to ./app/database/migrations, and you will see two more files. This is the database migration file. After a while, we will operate artisan to describe the two tables described by these two files. It becomes two real tables in the database. Don't worry, everything is automatic.

Below, modify in ***_create_articles_table.php:

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

Modify in ***_create_pages_table.php:

Copy code 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 moment to witness the miracle, run in the command line:

Copy code The code is as follows:

php artisan migrate

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

4. Models

Next we will get into the most powerful part of Laravel, Eloquent ORM, the place where productivity can really be improved. To borrow a word from Cook, Goose Meiziying!

We run the following statements on the command line to create two models:

Copy code 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 the core class Eloquent provided by Laravel.

5. Database filling

Run the following commands respectively:

Copy code 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/, which are our database filling files. Laravel provides automatic database filling, which is very convenient.

The generator uses FakerFactory as the random data generator by default, so we need to install this composer package, the address is https://packagist.org/packages/fzaninotto/faker, and install it together with the generator in require-dev. Please complete the specific installation by yourself. You can refer to Sentry and Generator. This is the first exercise.

Next, change these two files individually:

Copy code 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 to DatabaseSeeder.php so that Laravel will bring the two new seed files we added when seeding.

Copy code The code is as follows:

$this->call('ArticleTableSeeder');
$this->call('PageTableSeeder');

The next step is to actually fill the data into the database:

Copy code The code is as follows:

php artisan db:seed

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

Linux tutorial from installation to getting started (preferably with video)

itboba.com/taxonomy/term/263

proe50 64 crack proe50 Chinese cracked version proe40 installation tutorial proe40 installation video proe40 basic tutorial proe40 teaching video

What is this for? ? ?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/903473.htmlTechArticleLaravel 4 Basic Tutorial Installation and Getting Started, Laravel Basic Tutorial 0. Default Conditions This article assumes that you already have a well-configured PHP+MySQL operating environment, understand the basic knowledge of PHP website operation. Follow...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn