用命令行创建一个index名控制器文件 php artisan make:controller index
实例
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class index extends Controller { public function index(){ return 'index执行'; } }
运行实例 »点击 "运行实例" 按钮查看在线实例
2.用命令行创建一个check名中间件文件php artisan make:middleware check
实例
<?php namespace App\Http\Middleware; use Closure; class check { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { // 输出中间件的内容 echo 'hello middware <hr>'; return $next($request); } }
运行实例 »
点击 "运行实例" 按钮查看在线实例
3.打开I:\laravel58\app\Http\Kernel.php这个文件配置注册路由中间件,直接上图吧
4.路由代码
实例
<?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! | */ //给index路由加个中间件 //用法:路由后面写->middleware('中间件名称') Route::get('/index','index@index')->middleware('check');
运行实例 »
点击 "运行实例" 按钮查看在线实例
5.结果图:
总结:
这是最简单的方式,感觉不难,用命令创建两分钟就搞定了,作业没要求别的就这样写了,这作业真的不难!