Laravel implements user registration and login, laravel implements user registration_PHP tutorial

WBOY
Release: 2016-07-13 10:09:26
Original
1607 people have browsed it

Laravel implements user registration and login, laravel implements user registration

Laravel is the most elegant PHP framework. Many friends who learn PHP are coveting Laravel. Come realize your wish today. Let us start from scratch and use Laravel to implement the most common registration and login functions of web applications! All course source codes have been placed on Github: laravel-start. Race Start!

First, let’s clarify what we need for this course:

Laravel 4.2
Bootstrap 3.3
Laravel is the core part we care about, and Bootstrap is used to quickly set some front-end CSS styles.

1. Install Laravel

After a brief explanation, let’s go to the next step and install Laravel. Here we install it through Composer. Open the command line terminal and execute:

Copy code The code is as follows:

cd Sites

Sites is the root directory of the web application. You can change it to your own root directory as needed, and then execute:

Copy code The code is as follows:

composer create-project laravel/laravel laravel

Laravel is the name of your application directory. You can choose a name you like. After executing the above command, wait for a while (after all, Internet speed is a big problem in China). After the installation is completed, you will get this bunch of directories:

We mainly operate three directories: models, controllers and views: this is the composition of MVC!

2. Install Bootstrap

Then execute from the command line:

Copy code The code is as follows:

cd laravel/public/packages

Laravel here corresponds to the application directory above. If you used another name during installation, please change it accordingly. Go to the packages directory to install Bootstrap and execute it directly on the command line:

Copy code The code is as follows:

bower install bootstrap

This is faster, and after this is downloaded, you will get the latest stable version of Bootstrap. Bower_components/bootstrap/dist/ in the packages directory contains Bootstrap's css, js, and fonts, three style files, js, and font files that we often use during the development process. After success you will see this:

Note: The tool bower used here is responsible for managing some front-end packages.
At this point, our preliminary work is ready. But before proceeding to the next step, we must first ensure that our laravel/app/storage directory has corresponding write permissions, so return to the laravel directory. If you have not touched the command line after installing bower, you can directly pass:

Copy code The code is as follows:

cd ../../

Go back to the laravel directory and execute:

Copy code The code is as follows:

chmod -R 755 app/storage

After this step is completed, we can enter the real development stage.

3. Configure the database and create tables:

Before starting the configuration, we are going to create a database for our laravel application, I will name it laravel-start,

Then open the app/config/database.php file in the editor and fill in the corresponding database configuration items, such as:

Copy code The code is as follows:

'default' => 'mysql',
// Database connection
'connections' => array(
'mysql' => array(
         'driver'     => 'mysql',
                                                                                                                                                                                    'database' => 'laravel-start',
         'username' => 'root',
          'password' => '',
'charset' => 'utf8',
         'collation' => 'utf8_unicode_ci',
         'prefix'      => ),

After connecting to the database, you have to create a Users table. You can create the Users table directly in the database, or you can use Laravel's artisan to create it. Here we use Laravel's artisan to create the table, and learn a little bit about Laravel along the way. Knowledge of migrate. Execute the following statement:

php artisan migrate:make create-users-table
The above command will create a migrate file (the file is located in the app/database/migrations directory). The name of this file is create-users-table. Then we can create the Users table by editing the migrate file we just generated.

Copy code The code is as follows:

public function up() {
Schema::create('users', function($table){
          $table->increments('id');
$table->string('username', 20);
$table->string('email', 100)->unique();
$table->string('password', 64);
$table->string('remember_token',62)->default('default');
$table->timestamps();
        });
}

The above method uses laravel's Schema Builder class. The above code uses the up() method to create a users table. There are 5 fields in this table: id auto-increment, username length within 20, and email length within 100 And it is unique. The password length should be within 64. Remember_token is to make it more convenient and practical when logging in. Laravel will automatically fill in the token value, but at the beginning you must set a default value, timestamp the current timestamp. One thing we need to pay attention to here is: It is best to add the following code to down() in case we need to delete the Users table one day.

Copy code The code is as follows:

public function down()
{
Schema::drop('users');
}

After doing the above, execute the following magical command:

Copy code The code is as follows:

php artisan migrate

There are pictures and the truth:

Finally, we have finished the prelude and can officially come to Laravel.

4. Start the service and try it

Execute directly in the laravel directory:

Copy code The code is as follows:

php artisan serve

Open the browser, enter localhost:8000, press Enter, Bingo!
OK, give yourself thirty seconds of applause first, if you have successfully reached this point. Congratulations, you have entered the door of Laravel, we will have more surprises to come...

5. Create a public view

Okay, let’s start now. First create a layouts folder under the app/views/ folder, then create a new php file under this folder, name it main.blade.php, and write in this file The following codes:

Copy code The code is as follows:




         
          
                                                                                                                      alone




PS: The layouts folder is usually used to store the functional parts of the view file, such as the header
and tail
of some web pages. Here is where the header
part is stored

Think the name of main.blade.php is strange? Don’t worry, Laravel’s view file naming follows the rules of filename.blade.php, because Laravel is parsed by the Blade template engine. You don’t need to go into details, just name the view file according to the above naming rules

Add CSS styles to view files:

Copy code The code is as follows:




         
          
                                                                                                                      alone {{HTML::style('packages/bower_components/bootstrap/dist/css/bootstrap.min.css') }}
{{ HTML::style('css/main.css')}}





Yes, just add two lines of code based on the original main.blade.php; then we create our main.css, which is mainly used to put our own defined styles. Create a css folder under the public folder, create the main.css file in the css folder, and you're done.

Add navigation bar. Add the following code to the tag of the main.blade.php file:

Copy code The code is as follows:

          

Copy code

The code is as follows:

          
                @if(Session::has('message'))                

{{ Session::get('message') }}

               @endif
                       




In order to present this feedback information to the user, we have to use the Session::get('message') method. Of course, we have to first logically determine whether the message exists, so a simple if judgment is used here.

The format of if in the blade engine view is

Copy code

The code is as follows:

@if(conditions) #code... @endif


Is it over here? NO, if it ends here, how are other view files inserted between in main.blade.php? So, don’t forget another important thing: {{ $content }}, so the above code becomes like this:

Copy code

The code is as follows:


          

          @if(Session::has('message'))
            

{{ Session::get('message') }}


         @endif
            {{ $content }}
                       

{{ $content }} here represents the content of other view files. You can understand other views as a string, but this string is very long and happens to contain HTML tags. That’s all. You'll get a taste of this idea below.

After creating our public view main.blade.php, we first add our CSS style to main.css:

Copy code The code is as follows:

body {
​​ padding-top: 60px;
}
.form-signup, .form-signin {
       margin: 0 auto;
}

Because we used

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!