里面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
]);
//================路由自動反射================
$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]);
}
}
}
//==================================================================================
剛剛給webman加了個(gè)自動路由插件,可以用這個(gè)插件,使用更簡單,不用做任何配置。如果你想定制某個(gè)路由,直接在config/route.php
直接定義即可,自動路由組件會自動忽略它,使用你的自定義配置。
安裝
composer require webman/auto-route
記得刪除 config/route.php
里自動路由的腳本
插件文檔地址 http://wtbis.cn/plugin/17
好的 ,還有一件事。我在群里天天聊天, 我想要個(gè)管理員。這跟技術(shù)好不好沒關(guān)系吧 那個(gè)管理員沒事就裝逼。我可是Webman的忠實(shí)粉絲
那個(gè)管理員 就是個(gè)相聲演員。
而且我天天都在 活躍群的氣氛 發(fā)布問題 引導(dǎo)話題的。群主可以看群聊記錄 我每天都在。但是那個(gè)管理就喜歡沒事找事。而且我也屏蔽他的
這個(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á)到基本要求吧,然后我們再聊。
walkor敞亮!
能接受不同的看法及意見,這就是我一直都比較喜歡workerman/webman的原因
以前用過一段swoole,還有某前端框架,這里面的人真的是聽不得一點(diǎn)不同意見,稍微講點(diǎn)不足直接一幫人懟你
跟李某人懟了一場后果斷不再接觸這個(gè)了,我能寫go,能寫rust,能寫elixir/erlang,犯不著一定要用sw
從舊有PHP項(xiàng)目(基本是laravel系)轉(zhuǎn)webman也簡單便捷,