次日 第一步 学习使用命令的方式建立视图模板 php artisan generate:view admin._layouts.default 命令解释 生成视图模板文件,文件存放于视图目录下 /view /admin /_layouts default.blade.php // 解释一下,这个是模板文件,若是是用命令生成的那么内容是空的,须要手动去修改. 页面修改(admin/_layouts/default.blade.php) <!doctype html> <html> <head> <meta charset="utf-8"> <title>Learn Laravel 4</title> <!-- 从外部引入资源 --> @include('admin._partials.assets') </head> <body> <div class="container"> <div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar-inner"> <div class="container"> <a class="brand" href="{{ URL::route('admin.pages.index') }}">Learn Laravel 4</a> <!-- 这个标记是用来外部引入资源的. --> @include('admin._partials.navigation') </div> </div> </div> <hr> <!-- 这个标记是处理在模板继承的过程当中内容块的位置 --> @yield('main') </div> </body> </html> 第二步,用命令生成登陆模板 php artisan generate:viw admim.auth.login 编辑内容 (admin/auth/login.blade.php) 注意注释代码的书写方式. {{-- 这个就是模板继承的标记,同php的继承是同样的. --}} @extends('admin._layouts.default') {{-- 继承了一个基本的布局模板--}} {{-- 这个标记会查询父类模板中@yield的位置,以下代码到@stop为止,都会被父类模板中的@yield('main') 替换.--}} @section('main') <div id="login" class="login"> {{--开启表单功能 --}} {{ Form::open() }} @if ($errors->has('login')) <div class="alert alert-error">{{ $errors->first('login', ':message') }}</div> @endif <div class="control-group"> {{--表单的字段 --}} {{ Form::label('email', 'Email') }} <div class="controls"> {{ Form::text('email') }} </div> </div> <div class="control-group"> {{--表单字段--}} {{ Form::label('password', 'Password') }} <div class="controls"> {{ Form::password('password') }} </div> </div> <div class="form-actions"> {{--表单的提交--}} {{ Form::submit('Login', array('class' => 'btn btn-inverse btn-login')) }} </div> {{--关闭表单--}} {{ Form::close() }} </div> @stop 第三步,初步探视控制器 在app/controller 下新建文件夹admin 使用命令来生成控制 php artisan generate:controller admin/AuthController 初次生成,会看到控制器内有基本的crud操做的method_name ,能够保留, 如今尝试写一个登陆的控制器 <?php // 这个文件的命名空间 namespace App\Controllers\Admin; // 这个是,以下操做会使用到的对象. use Auth, BaseController, Form, Input, Redirect, Sentry, View; class AuthController extends BaseController { /** * 显示登陆页面 * @return View */ public function getLogin() { // 使用view对象的make方法,生成登陆页面 return View::make('admin.auth.login'); } /** * POST 登陆验证 * @return Redirect */ public function postLogin() { // 接受从post传递过来的数据, $credentials = array( 'email' => Input::get('email'), 'password' => Input::get('password') ); // 异常处理,若是鉴权成功,使用重定向跳转,不然抛出异常. try { $user = Sentry::authenticate($credentials, false); if ($user) { return Redirect::route('admin.pages.index'); } } catch(\Exception $e) { return Redirect::route('admin.login')->withErrors(array('login' => $e->getMessage())); } } /** * 注销 * @return Redirect */ public function getLogout() { Sentry::logout(); return Redirect::route('admin.login'); } 第四步 仅仅有控制器还不可以去访问,还的须要路由来处理 Route::get('admin/logout', array('as' => 'admin.logout', 'uses' => 'App\Controllers\Admin\AuthController@getLogout')); // 使用get的方式去访问, Route::get('admin/login', array('as' => 'admin.login', 'uses' => 'App\Controllers\Admin\AuthController@getLogin')); Route::post('admin/login', array('as' => 'admin.login.post', 'uses' => 'App\Controllers\Admin\AuthController@postLogin')); Route::group(array('prefix' => 'admin', 'before' => 'auth.admin'), function() { Route::any('/', 'App\Controllers\Admin\PagesController@index'); Route::resource('articles', 'App\Controllers\Admin\ArticlesController'); Route::resource('pages', 'App\Controllers\Admin\PagesController'); }); // 路由过滤 Route::filter('auth.admin', function() { if ( ! Sentry::check()) { return Redirect::route('admin.login'); } }); // 详细的路由知识能够看底层代码. 此时能够用路由来访问网站了, 若是出现错误 ***类不存在等问题,能够使用命令 composer dump-autoload 来更新autoload.php 文件 若是还提示其余的页面文件找不到,参考上述方法,自行生成 第五步: 生成填充数据 执行命令 php artisan generate:seed sentry <?php // Composer: "fzaninotto/faker": "v1.3.0" use Faker\Factory as Faker; class SentryTableSeeder extends Seeder { public function run() { // run方法内的内容是手动写上去的,请注意.自动生成的内容不是这样的. DB::table('users')->delete(); DB::table('groups')->delete(); DB::table('users_groups')->delete(); Sentry::getUserProvider()->create(array( 'email' => 'oo@xx.com', 'password' => "ooxx", 'first_name' => 'OO', 'last_name' => 'XX', 'activated' => 1, )); Sentry::getGroupProvider()->create(array( 'name' => 'Admin', 'permissions' => ['admin' => 1], )); // 将用户加入用户组 $adminUser = Sentry::getUserProvider()->findByLogin('oo@xx.com'); $adminGroup = Sentry::getGroupProvider()->findByName('Admin'); $adminUser->addGroup($adminGroup); } } 在app/databases/seed/databaseseeder.php 中添加 $this->call('SentrySeeder'); 随后执行命令 php artisan db:seed 数据已填充,能够尝试去登陆.