Home> php教程> PHP开发> body text

Laravel implements user registration and login

高洛峰
Release: 2016-12-27 10:43:53
Original
3424 people have browsed it

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 of all, 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:

cd Sites
Copy after login

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

composer create-project laravel/laravel laravel
Copy after login

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 installation, you will get this bunch of directories:

Laravel implements user registration and login

Our main operations Three directories: models, controllers and views: this is the composition of MVC!

2. Install Bootstrap

and then execute it from the command line:

cd laravel/public/packages
Copy after login

The laravel here corresponds to the application directory above. If you use another name when installing , please replace it accordingly. Go to the packages directory to install Bootstrap and execute it directly on the command line:

bower install bootstrap
Copy after login

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:

Laravel implements user registration and login

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:

cd ../../
Copy after login

Go back to the laravel directory, and then execute:

chmod -R 755 app/storage
Copy after login

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

3. Configure the database and create tables:

Before starting the configuration, we need to create a database for our laravel application. I named it laravel-start,

Laravel implements user registration and login

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

'default' => 'mysql', // 数据库连接 'connections' => array( 'mysql' => array( 'driver' => 'mysql', 'host' => '127.0.0.1', 'database' => 'laravel-start', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ),
Copy after login

After connecting to the database, you must 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 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.

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

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, The length of email should be within 100 and unique, and the length of password 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.

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

After doing the above, execute the following magical command:

php artisan migrate
Copy after login

There are pictures and the truth:

Laravel implements user registration and login

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

4. Start the service to try

Execute directly in the laravel directory:

php artisan serve
Copy after login

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 come with more surprises one by one...

5. Create a public view

好了,我们现在开始了,首先在app/views/文件夹下创建一个layouts文件夹,再再这个文件夹下新建一个php文件,命名为main.blade.php,在这个文件里写上下面这些代码:

     发现Laravel 4之美 
   
Copy after login

PS:layouts文件夹通常用来存放视图文件的功用部分,比如一些网页的头部

和尾部
,这里就是存放了头部
部分
感觉main.blade.php的名字很奇怪?不用担心,Laravel的视图文件命名遵循filename.blade.php的规则,因为Laravel是用Blade这个模板引擎解析的,你不用深究,就照着上面的名字规则来命名视图文件就OK

为视图文件添加CSS样式:

     发现Laravel 4之美 {{HTML::style('packages/bower_components/bootstrap/dist/css/bootstrap.min.css') }} {{ HTML::style('css/main.css')}} 
   
Copy after login

没错,就是在原来的main.blade.php的基础上添加两行代码;然后我们来创建我们的main.css,这个主要是用来放我们自己定义的样式。在public文件夹下创建css文件夹,在css文件夹创建main.css文件,大功告成。

添加导航栏。在main.blade.php文件的标签中加上以下代码:

  
Copy after login

上面只是引用了一些简单的Bootstrap的class,也没什么难的,不用伤心。

到这里基本的功用部分就结束了,但是我们的追求从不会这么low,所以为了更好地与用户交互,我们希望在用户进行某个操作之后给出一些反馈,比如注册成功的时候说:少年,你已成功注册本站,恭喜恭喜。等,于是乎,我们再为main.blade.php添加一点点代码:

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

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

@endif
Copy after login

为了现实这些反馈信息给用户,我们得使用Session::get('message')方法,当然,我们得首先从逻辑上判断一下这个message是否存在,所以这里用了一个简单的if判断。

在blade引擎的视图中if 的使用格式是

@if(conditions) #code... @endif
Copy after login

到这里就结束了么?NO,如果到这里就结束的话,其他的视图文件是怎么插入main.blade.php的之间的呢?所以,不要忘了还有一个重要的事:{{ $content }},于是乎,上面的代码就变成了这样:

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

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

@endif {{ $content }}
Copy after login

{{ $content }}在这里就是表示其他的视图文件内容,你可以在理解上将其他的视图当作一个字符串来理解,只不过这个字符串很长,而且恰好包含了HTML标签而已。下面你将体会到这种想法。

创建完我们的公用视图main.blade.php后,我们先来为main.css添加我们的CSS样式:

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

因为我们在main.blade.php文件中使用了

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 Recommendations
    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!