国产+高潮+在线,国产 av 仑乱内谢,www国产亚洲精品久久,51国产偷自视频区视频,成人午夜精品网站在线观看

Webman 自動解析出來的控制器 使用路由時(shí)添加中間件無效

2337669928@qq.com

這是中間件

截圖

這是使用中間件的路由 (沒反應(yīng))

截圖

這是使用正常方式解析 (正常)

截圖


應(yīng)該怎么解決 ?

2382 4 1
4個(gè)回答

walkor 打賞

問題一

里面Route::any('login', 應(yīng)該改成Route::any('/login',,路由的第一個(gè)參數(shù)都要是 / 開頭。

Route::group('/admin', function () {
    Route::any('/login', [\app\controller\admin\Login::class, 'index']);
})->middleware([
    \app\middleware\Tokenauth::class
]);

問題二

自動路由里已經(jīng)給所有的控制器自動加了路由,所以你再次添加相同的路由會報(bào)錯(cuò)。如果你要手動給某些uri添加路由,就在自動路由里將其忽略,所以config/route.php里應(yīng)該是類似這樣的代碼。

<?php
use Webman\Route;

// 自動路由忽略以下uri
$ignore_list = [
    '/admin/login',
];

$dir_iterator = new \RecursiveDirectoryIterator(app_path());
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
    // 忽略目錄和非php文件
    if (is_dir($file) || $file->getExtension() != 'php') {
        continue;
    }

    $file_path = str_replace('\\', '/',$file->getPathname());
    // 文件路徑里不帶controller的文件忽略
    if (strpos($file_path, 'controller') === false) {
        continue;
    }

    // 根據(jù)文件路徑計(jì)算uri
    $uri_path = strtolower(str_replace('controller/', '',substr(substr($file_path, strlen(app_path())), 0, -4)));
    // 根據(jù)文件路徑是被類名
    $class_name = str_replace('/', '\\',substr(substr($file_path, strlen(base_path())), 0, -4));

    if (!class_exists($class_name)) {
        echo "Class $class_name not found, skip route for it\n";
        continue;
    }

    // 通過反射找到這個(gè)類的所有共有方法作為action
    $class = new ReflectionClass($class_name);
    $class_name = $class->name;
    $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);

    $route = function ($uri, $cb) use ($ignore_list) {
        if (in_array($uri, $ignore_list)) {
            return;
        }
        Route::any($uri, $cb);
        Route::any($uri.'/', $cb);
        echo "$uri " . json_encode($cb)."\n";
    };

    // 設(shè)置路由
    foreach ($methods as $item) {
        $action = $item->name;
        if (in_array($action, ['__construct', '__destruct'])) {
            continue;
        }
        // action為index時(shí)uri里末尾/index可以省略
        if ($action === 'index') {
            // controller也為index時(shí)可以uri里可以省略/index/index
            if (substr($uri_path, -6) === '/index') {
                $route(substr($uri_path, 0, -6), [$class_name, $action]);
            }
            $route($uri_path, [$class_name, $action]);
        }
        $route($uri_path.'/'.$action, [$class_name, $action]);
    }

}

// 自己的路由
Route::group('/admin', function () {
    Route::any('/login', [\app\controller\admin\Login::class, 'index']);
})->middleware([
    \app\middleware\Tokenauth::class
]);
  • 2337669928@qq.com 2022-03-06

    作者還會回答的時(shí)候 我自己寫了一個(gè)方案,作者看看哪個(gè)好

2337669928@qq.com
//================路由自動反射================
$RouteGroup = [
    "/admin" => [
        app\middleware\Tokenauth::class,
    ],
];

$dir_iterator = new \RecursiveDirectoryIterator(app_path());
$iterator = new \RecursiveIteratorIterator($dir_iterator);
foreach ($iterator as $file) {
    // 忽略目錄和非php文件
    if (is_dir($file) || $file->getExtension() != 'php') {
        continue;
    }
    $file_path = str_replace('\\', '/',$file->getPathname());
    // 文件路徑里不帶controller的文件忽略
    if (strpos($file_path, 'controller') === false) {
        continue;
    }
    // 根據(jù)文件路徑計(jì)算uri
    $uri_path = strtolower(str_replace('controller/', '',substr(substr($file_path, strlen(app_path())), 0, -4)));
    // 根據(jù)文件路徑是被類名
    $class_name = str_replace('/', '\\',substr(substr($file_path, strlen(base_path())), 0, -4));

    if (!class_exists($class_name)) {
        echo "Class $class_name not found, skip route for it\n";
        continue;
    }

    // 通過反射找到這個(gè)類的所有共有方法作為action
    $class = new ReflectionClass($class_name);
    $class_name = $class->name;
    $methods = $class->getMethods(ReflectionMethod::IS_PUBLIC);

    $route = function ($uri, $cb, $mid = []) {
        //echo "Route $uri [{$cb[0]}, {$cb[1]}]\n";
        if($mid != []){
            Route::any($uri, $cb)->middleware($mid);
            Route::any($uri.'/', $cb)->middleware($mid);
        }else{
            Route::any($uri, $cb);
            Route::any($uri.'/', $cb);
        }

    };

    // 設(shè)置路由
    foreach ($methods as $item) {
        $action = $item->name;
        $__Class__ = substr($uri_path,0,strpos($uri_path,'/',1));
        if (in_array($action, ['__construct', '__destruct'])) {
            continue;
        }
        // action為index時(shí)uri里末尾/index可以省略
        if ($action === 'index') {
            // controller也為index時(shí)可以uri里可以省略/index/index
            if (substr($uri_path, -6) === '/index') {
                $route(substr($uri_path, 0, -6), [$class_name, $action]);
            }
            if(array_key_exists($__Class__,$RouteGroup)){
                $route($uri_path, [$class_name, $action],$RouteGroup[$__Class__]);
            }else{
                $route($uri_path, [$class_name, $action]);
            }
        }

        if(array_key_exists($__Class__,$RouteGroup)){
            $route($uri_path.'/'.$action, [$class_name, $action],$RouteGroup[$__Class__]);
        }else{
            $route($uri_path.'/'.$action, [$class_name, $action]);
        }
    }

}
//==================================================================================
walkor 打賞

剛剛給webman加了個(gè)自動路由插件,可以用這個(gè)插件,使用更簡單,不用做任何配置。如果你想定制某個(gè)路由,直接在config/route.php直接定義即可,自動路由組件會自動忽略它,使用你的自定義配置。

安裝
composer require webman/auto-route

記得刪除 config/route.php 里自動路由的腳本

插件文檔地址 http://wtbis.cn/plugin/17

  • 2337669928@qq.com 2022-03-06

    好的 ,還有一件事。我在群里天天聊天, 我想要個(gè)管理員。這跟技術(shù)好不好沒關(guān)系吧 那個(gè)管理員沒事就裝逼。我可是Webman的忠實(shí)粉絲

  • 2337669928@qq.com 2022-03-06

    你看我的回答

  • 2337669928@qq.com 2022-03-07

    裝不了 沒有這個(gè)東西

2337669928@qq.com

截圖
截圖
截圖
截圖
那個(gè)管理員 就是個(gè)相聲演員。

而且我天天都在 活躍群的氣氛 發(fā)布問題 引導(dǎo)話題的。群主可以看群聊記錄 我每天都在。但是那個(gè)管理就喜歡沒事找事。而且我也屏蔽他的

  • walkor 2022-03-07

    截圖
    這個(gè)回復(fù)沒看出哪里裝逼哈。再說誰還沒裝過,我之前也裝,但是不代表不能勝任管理員哈。

    如果說目前我對webman的貢獻(xiàn)第一,tinywan貢獻(xiàn)絕對就是第二(看本頁右上角月貢獻(xiàn)榜),tinywan提交了大量代碼包括大量的插件(昨天還在貢獻(xiàn)代碼文檔),提出建設(shè)性意見,給webman補(bǔ)充文檔,寫分享、在外積極宣傳webman,寫技術(shù)博客、教程等、不定時(shí)的捐贈webman/workerman,并且積極回復(fù)社區(qū)的各種提問。你說他不能勝任,那誰能呢?

    管理員的基本要求:技術(shù)好,對社區(qū)有一定貢獻(xiàn)(只會提問不算),人品好(最起碼不背后說別人壞話)。
    如果你想當(dāng)管理先達(dá)到基本要求吧,然后我們再聊。

  • nitron 2022-03-07

    walkor敞亮!
    能接受不同的看法及意見,這就是我一直都比較喜歡workerman/webman的原因
    以前用過一段swoole,還有某前端框架,這里面的人真的是聽不得一點(diǎn)不同意見,稍微講點(diǎn)不足直接一幫人懟你
    跟李某人懟了一場后果斷不再接觸這個(gè)了,我能寫go,能寫rust,能寫elixir/erlang,犯不著一定要用sw
    從舊有PHP項(xiàng)目(基本是laravel系)轉(zhuǎn)webman也簡單便捷,

  • li914 2022-03-07

    哈哈哈 感覺不需要群管理員

年代過于久遠(yuǎn),無法發(fā)表回答
??