Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to install Laravel 4

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to install Laravel 4". In daily operation, I believe many people have doubts about how to install Laravel 4. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to install Laravel 4". Next, please follow the editor to study!

0. Default condition

By default in this article, you already have a well-configured PHP+MySQL operating environment and understand the basics of PHP website operation. After following this tutorial, you will get a basic simple blog system that includes 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 stopped in the first step of learning Laravel, installation. Not because of the complexity of installing tutorials, but because of [well-known reasons]. Here I recommend a full composer Chinese image: http://pkg.phpcomposer.com/. It is recommended to "modify the configuration file of composer" mode. I tested with this image when I was 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 image configuration is complete, change to the directory where you want to place the website, and run the command:

The copy code is as follows:

Composer create-project laravel/laravel learnlaravel

Then, wait a moment, a folder called learnlaravel will appear in the current directory, and if you access the learnlaravel/public/ directory through a browser, it will almost always display Error in exception handler. This is because the learnlaravel/app/storage directory does not have 777 permissions. If you set the permissions, you can see the page as shown below:

Congratulations on the successful installation of Laravel!

Students who do not want to configure the image can install the artifact by using the famous super super in the Laravel world: https://github.com/overtrue/latest-laravel

two。 Installation and configuration of necessary plug-ins

We use the famous Sentry plug-in to build a permission verification system such as login.

Open. / composer.json and change to:

The copy code is as follows:

"require": {

"laravel/framework": "4.2.*"

"cartalyst/sentry": "2.1.4"

}

Then, run the command in the project root directory

The copy code is as follows:

Composer update

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

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:

The copy code is as follows:

"require-dev": {

"way/generators": "~ 2.0"

}

Put it under "require".

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

The copy code is as follows:

'Way\ Generators\ GeneratorsServiceProvider'

Once the installation is complete, run php artisan on the command line and you can see many new features brought by this plug-in.

Some people may ask, why is the use of domestic images still so slow? In fact, the slowest place for composer in update is not downloading, but the dependency resolution before downloading. As Laravel relies on a lot of composer packages, and the execution speed of PHP scripts is relatively slow, it is normal for update to wait two or three minutes at a time, 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" entry in "connections" to the configuration we need. Here is my configuration:

The copy code is as follows:

'mysql' = > array (

'driver' = >' mysql'

'host' = >' localhost'

'database' = >' laravel'

'username' = >' root'

'password' = >' password'

'charset' = >' utf8'

'collation' = >' utf8_unicode_ci'

'prefix' = >' l4mom'

),

Prefix is the table prefix. This Laravel will help us maintain it automatically. Don't worry if you write it down boldly.

At this point, you need to go to the database to create the database, and then enter it on the command line:

The copy code is as follows:

Php artisan migrate-package=cartalyst/sentry

When the execution is complete, you have five tables in your database, which are created by sentry itself. For more information on the configuration of sentry under Laravel4, please see https://cartalyst.com/manual/sentry#laravel-4. Let me outline this:

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

The copy code is as follows:

'Cartalyst\ Sentry\ SentryServiceProvider'

'Sentry' = > 'Cartalyst\ Sentry\ Facades\ Laravel\ Sentry'

This is the end of the database configuration for the permissions system.

Our simple blog system will have two elements, Article and Page. Let's create articles and pages data tables and run them on the command line:

The copy 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, when you go to. / app/database/migrations, you will see that there are two more files, that is, the database migration file. After a while, we will operate artisan to turn the two tables described by these two files into two real tables in the database. Rest assured, everything is automatic.

Next, modify it in * * _ create_articles_table.php:

The copy 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 the following in * * _ create_pages_table.php:

The copy 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 ()

});

Here is the moment to witness a miracle, run on the command line:

The copy code is as follows:

Php artisan migrate

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

4. Model Models

Next we'll touch on the most powerful part of Laravel, Eloquent ORM, where productivity is really improved, to borrow Cook's words, Goose Mei Ying!

We run the following statement on the command line to create two model:

The copy code is as follows:

Php artisan generate:model article

Php artisan generate:model page

At this point, 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:

The copy code is as follows:

Php artisan generate:seed page

Php artisan generate:seed article

At this point, two new files appear under. / app/database/seeds/, which is our database fill 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 the composer package at https://packagist.org/packages/fzaninotto/faker, which can be installed in require-dev along with generator. Please complete the specific installation by yourself, you can refer to Sentry and Generator, this is the first exercise.

Next, change the two files separately:

The copy 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 these two new seed files with it when seed.

The copy code is as follows:

$this- > call ('ArticleTableSeeder')

$this- > call ('PageTableSeeder')

Now it's time to actually populate the database with data:

The copy 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, 10 rows of article and 10 rows of page.

At this point, the study on "how to install Laravel 4" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report