Beginner's Metamorphosis: teach You step-by-step to create a simple forum system based on LARAVEL5 (1)

Source: Internet
Author: User
Tags php website

The sample code for this tutorial is shown in:

This tutorial Exchange group: 96094083

The quickest way to get stuck in any place is to look at my sample code or put it in a communication group.

Laravel 5 Chinese documents:

1. http://laravel-china.org/docs/5.0

2. http://www.golaravel.com/laravel/docs/5.0/

This project ultimately

Default condition

This article by default you already have the well-configured PHP + MySQL running environment, understand the basic knowledge of PHP website operation. Following this tutorial, you will get a basic simple Luntan 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 (such as C:\\wwwroot,/library/webserver/documents/,/var/www/html,/etc/nginx/html, and so on) and run the command:

Composer Create-project Laravel/laravel Learnlaravel5 5.0.22

Then, wait a moment, a folder called Learnlaravel5 appears in the current directory.

Then rename the Learnlaravel5 folder to Luntan.

This series of tutorials uses the Laravel 5.2 version, the 5.1 version removes the elements (Auth system) that are mainly explained in this series of tutorials and does not recommend 5.1来 learning. This series of tutorials is an introductory tutorial designed to clarify the basic use of Laravel and avoid putting the cart before the horse.

Open Folder apache-"conf=" extra= "http-vhosts.conf

Then, at the end of the Apache virtual Address profile http-vhosts.conf, configure the Web site root directory to luntan/public.

Add the following at a later
DocumentRoot "D:\ampp\htdocs\phpb\luntan\public" ServerName www.bbs.com serveralias bbs.com



Then use Notepad to open the Hosts file in the folder C:\Windows\System32\drivers\etc

Add the following on the last side of the file

127.0.0.1      www.bbs.com127.0.0.1      bbs.com


If you still do not configure, recommend to learn to configure, online data a lot. Note that you should always configure to ***/luntan/public.

Restart Apache Server


Using your browser to access your configured address, you will see the following screen (my locally configured address is http://www.bbs.com):

Then congratulations to you ~ Laravel 5 Installation Success!

Do not want to configure the image of the classmate, you can use the Laravel realm very famous Ang Sul to engage in the installation artifact: Https://github.com/overtrue/latest-laravel

3. Database Setup and Migration

Laravel 5.2 Change the location of the database configuration to ' learnlaravel5/.env ', open this file, edit the following four items and modify it to the correct information:

Db_host=localhostdb_database=luntandb_username=rootdb_password=password

It is recommended to create a new database named Luntan, which is recommended for direct operation with the root account for easy learning.

Laravel has prepared the Auth section of the migration for us, run the following command to perform the database migration operation:

PHP Artisan Migrate

The results are as follows:

If you run a command error, check the database connection settings.

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 sentence: Goose sister English!

Run a command to create a post, classify, and comment on the corresponding model class:

PHP Artisan Make:model Article
PHP Artisan Make:model Category
PHP Artisan Make:model Comment

> Laravel 4, we used the Generator plugin to create a new Model. Now, Laravel 5 has integrated Generator into the Artisan.

Now, Artisan helped us to create two files under ' learnlaravel5/app/', ' article.php ', comment.php and ' category.php ', which are three Model classes, they all inherit Laravel Eloquent provides the Model class ' Illuminate\database\eloquent\model ', and is all under the ' \app ' namespace. It is necessary to emphasize that creating a file from the command line is no different from creating a file yourself, and you can try to create these three 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 (which can, of course, correspond to multiple).

If you turn from the other frame, may be here a piece of the Model part is not suitable, no way, because eloquent is too powerful, really nothing to do, inherit the eloquent class can achieve a lot of many functions.

If you want to learn more about eloquent, you can read the series: in-depth understanding of Laravel eloquent (a)--basic concepts and usage

Next, the article, category and comment classes corresponding to the articles, categories tables and comments tables of the database migration, into the ' luntan/database/migrations ' folder.

Modify in ***_create_articles_table.php:

 
  
Use Illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;

Class Createarticlestable extends migration
{
/**
* Run the migrations.
*
* @return void
*/
Public function up ()
{
Schema::create (' articles ', function (Blueprint $table)
{
$table->increments (' id ');
$table->string (' user_id ');
$table->string (' title ');
$table->text (' content ');
$table->integer (' category ')->default (0);
$table->timestamps ();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
Public function Down ()
{
Schema::d rop (' articles ');
}
}

Modify in ***_create_categories_table.php:

 
  Increments (' id ');            $table->string (' name ');            $table->string (' color ');            $table->timestamps ();        });    }    /**     * Reverse the migrations.     *     * @return void     *    /Public Function down ()    {        Schema::d rop (' categories ');}    }



Modify in ***_create_comments_table.php:

 
  
Use Illuminate\database\schema\blueprint;
Use illuminate\database\migrations\migration;

Class Createcommentstable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
Public function up ()
{
Schema::create (' Comments ', function (Blueprint $table)
{
$table->increments (' id ');
$table->integer (' message_id ');
$table->integer (' message_user_id ');
$table->integer (' user_id ');
$table->integer (' state ')->default (0);
$table->string (' content ');
$table->timestamps ();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
Public function Down ()
{
Schema::d rop (' comments ');
}

}



Then execute the command:

PHP Artisan Migrate

After the success, articles tables, catogries and comments tables have appeared in the database, go to see it ~

5. Database Population seeder

Under ' luntan/database/seeds/', create a new ' articletableseeder.php ' file with the following content:

 
  
Use Illuminate\database\seeder;
Use app\article;

Class Articletableseeder extends Seeder {

Public Function Run ()
{
Db::table (' articles ')->delete ();

Message::create ([
' user_id ' = ' 1 ',
' title ' = ' an ' an ',
' content ' = ' son, to be satisfied with the Earth. ',
' Category ' = ' 1 ',
]);
}

}

Under ' luntan/database/seeds/', create a new ' catogrytableseeder.php ' file with the following content:

 
  Delete ();      Category::create ([      ' id ' = ' 1 ',        ' name '   = ' talk ',        ' color '    = ' 202, ', ') '      ;  }}



Then modify the ' databaseseeder.php ' in the same level directory:

$this->call (' Usertableseeder ');

This sentence is

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

Then run the command to populate the data:

Composer Dump-autoloadphp Artisan Db:seed

Take a look at the articles table and the Categories table, are there more rows of data?

  • 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.