中间件为过滤进入应用程序的HTTP请求提供了一种方便的机制。例如,Laravel 内置了一个中间件来验证用户的身份认证。如果用户没有通过身份认证,中间件会将用户重定向到登录界面。但是,如果用户被认证,中间件将允许该请求进一步进入该应用。
当然,除了身份认证以外,还可以编写另外的中间件来执行各种任务。例如:CORS 中间件可以负责为所有离开应用的响应添加合适的头部信息;日志中间件可以记录所有传入应用的请求。
Laravel 自带了一些中间件,包括身份验证、CSRF 保护等。所有这些中间件都位于 app/Http/Middleware 目录。
web.php
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::group([
'middleware' => ['api', 'CheckParam'],
'prefix' => 'h5',
//'namespace' => '\App\Http\Controllers'
], function () {
Route::get('/', function () {
return view('welcome');
});
Route::get('/blog', '\Modules\Blog\Http\Controllers\BlogController@index');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::redirect('/testtest', '/', 301);
//Route::get('user/{id}', function ($id) {
// return 'User '.$id;
//});
//Route::get('show/{id}/{classid}', 'HomeController@index');
//Route::get('user/{name}', function ($name) {
// //
//})->where('name', '[A-Za-z]+');
Route::get('user/{username}', 'HomeController@index')->where('username', '[a-z]+');
Route::get('show/{id}', 'HomeController@show');
//Route::get('show/{id}', 'HomeController@show')->where('id', '[0-9]+');
//Route::get('user/{id}/{name}', function ($id, $name) {
// //
//})->where(['id' => '[0-9]+', 'name' => '[a-z]+']);
});
?>
app/http/kernel.php
<?php
protected $routeMiddleware = [
'CheckParam' => \App\Http\Middleware\CheckParam::class,
];
?>
HomeController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
* @return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard.
* @return \Illuminate\Http\Response
*/
public function index($username)
{
echo "username=>".$username;
//return view('home');
}
public function show($id){
echo "show_id=>".$id;
}
}
?>
app/http/middleware/CheckParam.php
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use ZM\ApiResponse\Facades\ApiResponse;
class CheckParam
{
/**
* Handle an incoming request.
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(isset($request->id) && !preg_match("/^[1-9][0-9]*$/",$request->id)){
echo "exit";exit;
//return ApiResponse::failure(10010, config('errcode.10010'));
}
if(isset($request->master_id) && !preg_match("/^[1-9][0-9]*$/",$request->master_id)){
//return ApiResponse::failure(10010, config('errcode.10010'));
}
if(isset($request->type) && !preg_match("/^[1,2]*$/",$request->type)){
//return ApiResponse::failure(10010, config('errcode.10010'));
}
return $next($request);
}
}
?>