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. 解决windows64位系统上安装mysql-python报错

    解决windows64位系统上安装mysql-python报错 2018年03月12日 13:08:24 一个CD包 阅读数:1231    版权声明:本文为博主原创文章,未经博主允许不得转载. ht ...

  2. Jupyter Notebook 下安装 PHP 内核

    我最近被强烈安利了 Jupyter Notebook 这个交互式笔记本.然后试用了它自带的 Python 内核后,这个应用整体给我的感觉很不错,就去搜索了下它所支持的其它内核 Jupyter Kern ...

  3. hdu2009 求数列的和【C++】

    求数列的和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  4. spring boot 中访问 REST 接口

    RestTemplate restTemplate = new RestTemplate(); Object result = restTemplate.getForObject("http ...

  5. 算是学完了《Servlet&JSP学习笔记》,立此存照

    我感觉从构架上来说,算是入门了, 终于可以正式进入SPRING的学习啦...爽 代码就不弄了,真的太多了...花了差不多两周呢..

  6. [ACM] POJ 1942 Paths on a Grid (组合)

    Paths on a Grid Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21297   Accepted: 5212 ...

  7. 使用Linq 查询数据 构建对象 select new{}

    linq 查询数据 /// <summary> /// 汽车品牌及车型 /// </summary> /// <returns></returns> p ...

  8. leetcode第一刷_Convert Sorted Array to Binary Search Tree

    晕.竟然另一样的一道题.换成sorted array的话.找到中间位置更加方便了. TreeNode *sortTree(vector<int> &num, int start, ...

  9. 高阶MapReduce_1_链接多个MapReduce作业

    链接MapReduce作业 1.      顺序链接MapReduce作业 顺序链接MapReduce作业就是将多个MapReduce作业作为生成的一个自己主动化运行序列,将上一个MapReduce作 ...

  10. HDU 2110-Crisis of HDU(母函数)

    Crisis of HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...