PHP自从5.3后似乎又热度又回升, 最近了解了一下PHP框架之一Laravel, 最近最新的版本已经是4.3  基本的结构这里不讲, 要了解可以在这里看文档 http://v4.golaravel.com/docs/4.2 , 下面只是记录一下身份验证的实现过程

 

/***********************

 *    注册 登录 登出 路由

 ***********************/

Route::get('home/login', function(){

    return View::make('home.login');

}); 

 

Route::post('home/login', 'HomeController@login');

 

Route::get('home/register', function(){

    return View::make('home.register');

});

 

Route::post('home/register', 'HomeController@register');

 

Route::get('home/logout', function(){

    Auth::logout();

    return Redirect::intended('/');

});

 

Laravel提供了一个MVC的机制,使用Eloquent ORM来处理数据库操作, 这里路由定义了HomeController控制器类里面的两个Action方法,一个是home/login, 一个是home/register, 分别处理来自 login.blade.php 和 register.blade.php里页面里的提交的数据。 离开动作home/logout没有使用控制器的动作, 而是直接用Auth类的方法Auth::logout,然后跳转到网站根目录

 

下面是register.blade.php页面的内容, 我们看到比较简单,使用了Blade模板,就是一个Form,提交地址,和一些表格元素。

@extends('home.master')

 

<h1>注册页面</h1>

@section('content')

{{  Form::macro('br', function(){

            return '<br/>';

    })

}}

 

{{ Form::open(array('action' => 'HomeController@register', 'method'=>'post')) }}

{{

 

    Form::label('name','姓名')." ".Form::text('name').Form::br().

 

    Form::label('email','邮箱')." ".Form::text('email').Form::br().

 

    Form::label('password','密码')." ".Form::password('password').Form::br().

 

    Form::label('confirm_pass','确认密码')." ".Form::password('confirm_pass').Form::br().

 

    Form::br().Form::submit('确定')

}}

{{ Form::close() }}

 

 

@endsection

 

下面也类似的是login.blade.php页面的内容

@extends('home.master')

 

<h1>登录页面</h1>

@section('content')

 

{{  Form::macro('br', function(){

            return '<br/>';

    })

}}

 

{{ Form::open(array('action' => 'HomeController@login', 'method'=>'post')) }}

{{ 

    Form::label('email','邮箱')." ".Form::text('email').Form::br().

    Form::label('password','密码')." ".Form::password('password').

 

    Form::submit('登录')

 

 

}}

{{ Form::close() }}

@endsection

 

然后提交后,根据路由规则,就来到HomeController控制器的 register() 方法和 login() 方法来处理

<?php

 

class HomeController extends BaseController {

 

public function  index()

{

    $page = Input::get('page',1); 

    $perPage = 5;

 

    $offset = ($page*$perPage)-$perPage;

    $all= User::all()->toArray();

    $vars['users'] = User::skip($offset)->take($perPage)->get();

    $vars['paginate'] = Paginator::make($all, count($all) , $perPage);

    return View::make('home.index', $vars);

}

 

 

public function  register()

{

    $user = new User();

    $user->fill( Input::all());

    $user->password = Hash::make($user->password);

    $user = User::create($user->toArray());

    if($user->id)

    {

            if(Auth::loginUsingId($user->id))

            {

                echo "Login success";

            }

            return Redirect::intended('/');

        

    }

 

}    

 

public function login()

{

    $email = Input::get('email');

    $password = Input::get('password');

    if (Auth::attempt(array('email' => $email, 'password' => $password)))

    {

        return Redirect::intended('/');

    }

    else

    {

        return 'Login failed!'.Link_to('home/login');

    }

}

 

}

 

这三个方法有点复杂,以后在写。

PHP 框架Laravel Eloquent 实现身份验证的更多相关文章

  1. PHP框架 Laravel Eloquent ORM 批量插入数据 && 批量更新目前没有

    foreach ($products as $v=>$a) { $count[] = array('product_name' => $a['name'], 'product_weight ...

  2. Shiro安全框架入门篇(登录验证实例详解与源码)

    转载自http://blog.csdn.net/u013142781 一.Shiro框架简单介绍 Apache Shiro是Java的一个安全框架,旨在简化身份验证和授权.Shiro在JavaSE和J ...

  3. (八)play之yabe项目【身份验证】

    (八)play之yabe项目[身份验证] 博客分类: 框架@play framework   添加身份验证 play提供了一个模块-Secure(安全模块),用来做身份验证 允许Secure模块 修改 ...

  4. ASP.NET Core 身份验证(一)

    前言 这篇文章我想带领大家了解一下 ASP.NET Core 中如何进行的身份验证,在开始之前强烈建议还没看过我写的 Identity 系列文章的同学先看一下. Identity 入门系列文章: Id ...

  5. 如何基于asp.net core的Identity框架在mysql上作身份验证处理

    首先了解这个概念,我一开始也是理解和掌握基本的概念,再去做程序的开发.Identity框架是微软自己提供,基于.net core平台,可拓展.轻量 级.面向多个数据库的身份验证框架.IdentityS ...

  6. webapi框架搭建-安全机制(二)-身份验证

    webapi框架搭建系列博客 身份验证(authentication)的责任是识别出http请求者的身份,除此之外尽量不要管其它的事.webapi的authentication我用authentica ...

  7. ASP.NET Core身份验证服务框架IdentityServer4-整体介绍

    一.整体情况 现代应用程序看起来更像这个: 最常见的相互作用: 浏览器与Web应用程序的通信 Browser -> Web App Web应用程序与Web API通信 基于浏览器的应用程序与We ...

  8. Laravel Eloquent使用小记

    原文地址:http://blog.onlywan.cc/14843810761202.html Laravel Eloquent使用小记 今天由于开发数据库业务中间层须要.開始研究Laravel El ...

  9. IdentityServer4 使用OpenID Connect添加用户身份验证

    使用IdentityServer4 实现OpenID Connect服务端,添加用户身份验证.客户端调用,实现授权. IdentityServer4 目前已更新至1.0 版,在之前的文章中有所介绍.I ...

随机推荐

  1. copy.c实现

    #cat copy.c #include <stdio.h> #include <stdlib.h> #include <string.h> int copyFil ...

  2. What is the difference between rhel 6 and rhel7

    What is the difference between rhel 6 and rhel7 difference rhel 6 RHEL 7 release date 10 NOV 2010 as ...

  3. const浅析

    前言 c++中使用到const的地方有很多, 而且const 本身也针对不同的类型可能有不同的含义, 比如对指针就有顶层和底层. 本节就是探讨关于C++中const的在不同的地方不同表现或含义. co ...

  4. 【数据结构】C语言栈的基本操作

    #include<stdio.h> #include<stdlib.h> #include<malloc.h> //定义节点 struct Node { int d ...

  5. 24.通过ngram分词机制实现index-time搜索推荐

    一.ngram和index-time搜索推荐原理     1.什么是ngram     假设有一个单词:quick,在5种长度下的ngram情况如下: ngram length=1,q u i c k ...

  6. img标签和background-image的区别和具体使用时机

    最近在使用图片过程中,纠结到底使用img标签还是使用background-image属性,翻阅资料和百度后作出下列理解. 简单来说img是内容部分的东西,background-image是修饰性的东西 ...

  7. 解决git pull每次提示输入账号密码的问题

    每次用git同步代码的时候,都会提示输入账号密码,很麻烦,费时间,所以找了一种可以免去每次都要输入账号密码的方法 1. git bash进入你的项目目录 2. 输入以下命令会在配置文件里添加信息,作用 ...

  8. 关于PyQt5,在pycharm上的安装步骤及使用技巧

    前序 之前学习了一款GUI图形界面设计的Tkinter库,但是经大佬的介绍,PyQT5全宇宙最强,一脸的苦笑 毫不犹豫的选择转战PyQT5,在学习之前需要先安装一些必须程序,在一番查阅后,发现PyQt ...

  9. [poj3735] Training little cats_矩乘快速幂

    Training little cats poj-3735 题目大意:给你n个数,k个操作,将所有操作重复m次. 注释:三种操作,将第i个盒子+1,交换两个盒子中的个数,将一个盒子清空.$1\le m ...

  10. [Jest] Use property matchers in snapshot tests with Jest

    With the right process in place, snapshot tests can be a great way to detect unintended changes in a ...