Blogger Information
Blog 37
fans 0
comment 1
visits 29615
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
创建一个路由中间件,并通过访问url地址来触发中间件输出一名话:‘hello middware’-2019-11-08
H先生
Original
1001 people have browsed it

实例



1.png

<?php
namespace App\Http\Middleware;

use Closure;

class CheckAge
{


    public function handle($req,Closure $next){
        $username = 'admin';
        if ($username!='admin'){
            exit ('不旭旭看');
        }

//        echo 'checkAge中间件执行完毕';
      return  $next($req);
    }

}
?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

use App\Models\Director;

use Illuminate\Http\Request;

class Home extends Controller
{
    /*public function index(Director $users){
        echo '<pre>';
        $res = $users->cc();
        foreach($res as $k => $val){
            $res[$k] = (array)$val;
        }
        $users = Director::find(1);
        print_r($users->name);
    }*/

    /*public function index(Request $req){
        $id = $_GET['director'];
        $id = $req->director;
    }*/
    public function index(){
        echo 'hello middware';
    }
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \App\Http\Middleware\TrustProxies::class,
        \App\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
        // 自定义的中间件
        'checkage' => \App\Http\Middleware\CheckAge::class,
    ];

    /**
     * The priority-sorted list of middleware.
     *
     * This forces non-global middleware to always be in the given order.
     *
     * @var array
     */
    protected $middlewarePriority = [
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\Authenticate::class,
        \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authorize::class,
    ];
}

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?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::get('/', function () {
    return view('welcome');
});

Route::get('/304525', function () {
    // 读取数据
    $article = array('title'=>'2019国庆大阅兵','detail'=>'奖励上级阿萨德老费劲啊胜利大街爱上了就发了房间里睡大觉','auth'=>array('username'=>'admin','age'=>18));
    return view('article/detail',$article);
});

Route::get('/home','home@index')->middleware('checkage');

// 1.解析、home
// 2.$home = new Home();

// 3.调用$home->index();




Route::get('/article/detail',function (){
    return ('<div style="margin: 10px auto;text-align:center; font-weight: bold;color: red;">新闻列表</div>');
});

Route::get('/admin/home/index','admin\Home@index');

Route::get('/admin/home/lists','admin\Home@lists');

// 登录
Route::get('/admin/home/login','admin\Home@login');
Route::post('/admin/home/dologin','admin\Home@dologin');


Route::get('/dbtest/index','Dbtest@index');

Route::get('/director/index','Director@index');

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:合格
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post