laravel用json格式提交数据,验证层怎么写
1个回答
展开全部
首先确认,后台的用户表,我设计表叫做badmin,每个管理员有用户名(username),有昵称(nickname),有邮箱(email),有密码(password)
这里玩个花,使用laravel的migration来建立表(实际上可以用不着使用这个工具建立表)
1 安装好最基本的laravel框架
2 创建migration文件:
./artisan migrate:make create-badmin-table
3 发现app/database/migration/下面多了一个php文件:
2014_10_19_090336_create-badmin-table.php
4 往up和down里面增加内容;
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBadminTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('badmin', function($table)
{
$table->increments('id');
$table->string('nickname', 100)->unique();
$table->string('username', 100)->unique();
$table->string('email', 100)->unique();
$table->string('password', 64);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('badmin');
}
}
5 配置好local的database,app/config/local/database.php
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => ’test',
'username' => 'yejianfeng',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
);
6 创建数据表:
./artisan migrate --env=local
这个时候去数据库看,就发现多了一张badmin表,数据结构如下:
CREATE TABLE `badmin` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `badmin_nickname_unique` (`nickname`),
UNIQUE KEY `badmin_username_unique` (`username`),
UNIQUE KEY `badmin_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
要问这里为什么多出了create_at和update_at,这是laravel默认为每个表创建的字段,而且在使用Eloquent进行增删改查的时候能自动更新这两个字段
7 创建个Model:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Badmin extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'badmin';
protected $hidden = array('password');
public static $rules = [
'nickname' => 'require|alpha_num|min:2',
'username' => 'require',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
];
}
这里必须要implements UserInterface和RemindableInterface
8 把model和Auth关联上,修改app/config/auth.php
<?php
return array(
// 默认的用户验证驱动
// 可以是database或者eloquent
'driver' => 'eloquent',
// 只有驱动为eloquent的时候才有用
'model' => 'Badmin',
);
这里的driver可以是eloquent或者database,使用eloquent就告诉Auth组件说,用户认证类是Badmin这个类管的。这里的model是有命名空间的,就是说如果你的admin类是\Yejianfeng\Badmin,这里就应该改成’\Yejianfeng\Badmin’
9 好了,这个时间其实逻辑部分已经搭建完毕了,你已经可以在controller种使用
Auth::attempt(XXX) 做权限认证
Auth::user() 获取登录用户(一个Badmin类)
等。
10 下面要建立一个用户登录页面:
11 设置路由:
<?php
// 不需要登录验证的接口
Route::get('/', ['as' => 'user.login','uses'=>'UserController@getLogin']);
Route::get('user/login', ['as' => 'login', 'uses' => 'UserController@getLogin']);
Route::post('user/login', ['as' => 'login', 'uses' => 'UserController@postLogin']);
// 需要登录验证才能操作的接口
Route::group(array('before' => 'auth'), function()
{
Route::get('user/logout', ['as' => 'logout', 'uses' => 'UserController@getLogout']);
Route::get('user/dashboard', ['as' => 'dashboard', 'uses' => 'UserController@getDashboard']);
});
12 设置controller:
<?php
class UserController extends BaseController {
// 登录页面
public function getLogin()
{
return View::make('user.login');
}
// 登录操作
public function postLogin()
{
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
return Redirect::to('user/dashboard')
->with('message', '成功登录');
} else {
return Redirect::to('user/login')
->with('message', '用户名密码不正确')
->withInput();
}
}
// 登出
public function getLogout()
{
Auth::logout();
return Redirect::to('user/login');
}
public function getDashboard()
{
return View::make('user.dashboard');
}
// 添加新用户操作
public function getCreate()
{
return View::make('user.create');
}
// 添加新用户操作
public function postCreate()
{
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()){
$bAdmin = new Badmin();
$bAdmin->nickname = Input::get('nickname');
$bAdmin->username = Input::get('username');
$bAdmin->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
Response::json(null);
} else {
Response::json(['message' => '注册失败'], 410);
}
}
}
13 设置下filter,app/filter.php
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('/');
}
}
});
将这里认证失败后的地址转到/ 路径
14 设置views/user/login.blade.php
这里玩个花,使用laravel的migration来建立表(实际上可以用不着使用这个工具建立表)
1 安装好最基本的laravel框架
2 创建migration文件:
./artisan migrate:make create-badmin-table
3 发现app/database/migration/下面多了一个php文件:
2014_10_19_090336_create-badmin-table.php
4 往up和down里面增加内容;
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBadminTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('badmin', function($table)
{
$table->increments('id');
$table->string('nickname', 100)->unique();
$table->string('username', 100)->unique();
$table->string('email', 100)->unique();
$table->string('password', 64);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('badmin');
}
}
5 配置好local的database,app/config/local/database.php
<?php
return array(
'fetch' => PDO::FETCH_CLASS,
'default' => 'mysql',
'connections' => array(
'mysql' => array(
'driver' => 'mysql',
'host' => 'localhost',
'database' => ’test',
'username' => 'yejianfeng',
'password' => '123456',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
),
),
'migrations' => 'migrations',
);
6 创建数据表:
./artisan migrate --env=local
这个时候去数据库看,就发现多了一张badmin表,数据结构如下:
CREATE TABLE `badmin` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`nickname` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`username` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
`password` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `badmin_nickname_unique` (`nickname`),
UNIQUE KEY `badmin_username_unique` (`username`),
UNIQUE KEY `badmin_email_unique` (`email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
要问这里为什么多出了create_at和update_at,这是laravel默认为每个表创建的字段,而且在使用Eloquent进行增删改查的时候能自动更新这两个字段
7 创建个Model:
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class Badmin extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'badmin';
protected $hidden = array('password');
public static $rules = [
'nickname' => 'require|alpha_num|min:2',
'username' => 'require',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
];
}
这里必须要implements UserInterface和RemindableInterface
8 把model和Auth关联上,修改app/config/auth.php
<?php
return array(
// 默认的用户验证驱动
// 可以是database或者eloquent
'driver' => 'eloquent',
// 只有驱动为eloquent的时候才有用
'model' => 'Badmin',
);
这里的driver可以是eloquent或者database,使用eloquent就告诉Auth组件说,用户认证类是Badmin这个类管的。这里的model是有命名空间的,就是说如果你的admin类是\Yejianfeng\Badmin,这里就应该改成’\Yejianfeng\Badmin’
9 好了,这个时间其实逻辑部分已经搭建完毕了,你已经可以在controller种使用
Auth::attempt(XXX) 做权限认证
Auth::user() 获取登录用户(一个Badmin类)
等。
10 下面要建立一个用户登录页面:
11 设置路由:
<?php
// 不需要登录验证的接口
Route::get('/', ['as' => 'user.login','uses'=>'UserController@getLogin']);
Route::get('user/login', ['as' => 'login', 'uses' => 'UserController@getLogin']);
Route::post('user/login', ['as' => 'login', 'uses' => 'UserController@postLogin']);
// 需要登录验证才能操作的接口
Route::group(array('before' => 'auth'), function()
{
Route::get('user/logout', ['as' => 'logout', 'uses' => 'UserController@getLogout']);
Route::get('user/dashboard', ['as' => 'dashboard', 'uses' => 'UserController@getDashboard']);
});
12 设置controller:
<?php
class UserController extends BaseController {
// 登录页面
public function getLogin()
{
return View::make('user.login');
}
// 登录操作
public function postLogin()
{
if (Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))) {
return Redirect::to('user/dashboard')
->with('message', '成功登录');
} else {
return Redirect::to('user/login')
->with('message', '用户名密码不正确')
->withInput();
}
}
// 登出
public function getLogout()
{
Auth::logout();
return Redirect::to('user/login');
}
public function getDashboard()
{
return View::make('user.dashboard');
}
// 添加新用户操作
public function getCreate()
{
return View::make('user.create');
}
// 添加新用户操作
public function postCreate()
{
$validator = Validator::make(Input::all(), User::$rules);
if ($validator->passes()){
$bAdmin = new Badmin();
$bAdmin->nickname = Input::get('nickname');
$bAdmin->username = Input::get('username');
$bAdmin->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$user->save();
Response::json(null);
} else {
Response::json(['message' => '注册失败'], 410);
}
}
}
13 设置下filter,app/filter.php
Route::filter('auth', function()
{
if (Auth::guest())
{
if (Request::ajax())
{
return Response::make('Unauthorized', 401);
}
else
{
return Redirect::guest('/');
}
}
});
将这里认证失败后的地址转到/ 路径
14 设置views/user/login.blade.php
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询