Laravel 4 Series Getting Started tutorial (i)

Source: Internet
Author: User

Default conditions This article by default you already have a well-configured Php+mysql running environment, understand the basic knowledge of PHP Web site operation. Following this tutorial, you will get a basic simple blog system with login, and will learn how to use some powerful laravel plugins and composer packages (Laravel plugins are also composer packages).
Software version: PHP 5.4+,mysql 5.1+ This article does not recommend learning from people who do not understand PHP and MVC programming at all. This article is not a "step-by-Step with Me" tutorial. This article requires a certain mind to solve some large or small hidden tasks, in order to achieve a true understanding of the purpose of Laravel running logic. 1. Installation Many people were stopped in the first step of learning Laravel, installed. This is not because of the complexity of the installation tutorials, but because of "well-known causes." Here I recommend a composer full amount Chinese mirror: http://pkg.phpcomposer.com/. It is recommended to configure the "Modify composer Profile" mode. After the mirroring configuration is complete, switch to the directory where you want to place the site, run the command: Composer Create-project Laravel/laravel Learnlaravel 4.2.11 for the convenience of everyone, please use 4.2.11 temporarily Version to get started. Spring Festival Holiday I'll take the time to write about the Laravel 5 introductory tutorial. 4.2.11 Github is: Https://github.com/laravel/laravel/archive/v4.2.11.zip Then, wait a moment, there will be a folder called Learnlaravel in the current directory, At this point, if you access the learnlaravel/public/directory through your browser, the Error in exception handler will be displayed basically. , this is because the Learnlaravel/app/storage directory does not have 777 permissions, set permissions can congratulate you ~laravel installation success! Do not want to configure the image of the classmate, you can use the Laravel world is very famous super Super get to install Artifact: Https://github.com/overtrue/latest-laravel2. Necessary plug-in installation and configuration we use the famous Sentry plugin to build a login and other rights verification system. Open the./composer.json, change to: "Require": {"laravel/framework": "4.2.*", "Cartalyst/sentry": "2.1.4"}, then, Run the command composer update under the project root and wait a moment, it will prompt cartalyst/sentry 2.1.4 installation is complete. In the same vein, we will install a very powerful plugin for development, Way/generators, which is its name in the composer library. In COMPOSER.JSon added: "Require-dev": {"way/generators": "~2.0"}, and "require" sibling, placed below, not inside oh ~. Run composer Update, and then add the configuration in the appropriate location in the./app/config/app.php: ' Way\generators\generatorsserviceprovider ' installation is complete, run PHP on the command line Artisan, you can see the many new features that this plugin brings. Some people will ask, why use the domestic mirror or so slow? In fact composer in the update when the slowest place is not download, but download the dependency resolution, because Laravel relies on the composer package is very much, PHP script execution speed is relatively slow, so every update, such as two or three minutes is normal, habit is good. 3. Database setup and Migration database configuration file located in./app/config/database.php, we need to change the "MySQL" Item in "Connections" to the configuration we need. Here is 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 automatically maintenance, bold write Don't worry. At this point you need to go to the database to build this database, and then enter it on the command line: PHP artisan migrate--package=cartalyst/sentry After the completion of execution, your database has 5 tables, this is sentry self-established. Sentry configuration details under Laravel4 see https://cartalyst.com/manual/sentry#laravel-4, I'm talking about: in./app/config/app.php in the corresponding location Add the following two lines respectively: ' 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 articles and pages data table, command line run: PHP artisan migrate:make create_articles_table-- create=articlesphp Artisan Migrate:make create_pages_table--create=pages This time, go to./app/database/migrations, will see two more files, this is the database migration file, after a while we will operate artisan the two tables described in these two files into the real two tables in the database, rest assured that everything is automatic. Below, modified 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 ();}); Modified 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 is the time to witness the miracle, run on 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, really improve the productivity of the place, borrow Cook's words to say, goose sister English! We run the following statement at the command line to create two model:php artisan generate:model articlephp Artisan Generate:model page at this time, two files appeared under app/models/ Article.php and page.php, which are two Model classes, inherit the core class \eloquent provided by Laravel. It is necessary to emphasize that creating a file with a command line is no different from creating a file yourself, and you can try creating these two Model classes yourself. Model is the M in MVC, which translates 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 another frame, you may not be comfortable with a piece of the Model you've taken here., no way, because eloquent is too strong, really nothing to do, inherit the eloquent class can achieve a lot of many functions. See Eloquent Series Tutorial: in-depth understanding of Laravel eloquent (i)-Basic concepts and usage 5. Database fills run the following command, respectively: PHP artisan generate:seed pagephp Artisan generate:seed article at this time, two new files appeared under./app/database/seeds/, This is our database populate 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 https://packagist.org/packages/fzaninotto/faker, Install with generator in Require-dev. Specific installation Please do it yourself, you can refer to Sentry and generator, this is the first practice. Next, change these 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 ' and $faker- >paragraph ($nbSentences = 5), ' user_id ' = 1,]); then we need to add two rows to the databaseseeder.php, Let Laravel bring the two new seed files that we added to seed. $this->call (' Articletableseeder '); $this->call (' Pagetableseeder '); The real data is populated into the database: PHP Artisan DB: After the seed operation is completeGo to the database to see, the data has been filled in, article and page each 10 lines.

  

Laravel 4 Series Getting Started tutorial (i)

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.