前后端分離的TP項(xiàng)目,想轉(zhuǎn)webman,基礎(chǔ)的路由實(shí)現(xiàn)不了,不知道是不是用法錯(cuò)了。
"workerman/webman-framework": "^1.4.3",
PHP:8.0
OS: Ubuntu 20....
子域名配合nginx加上默認(rèn)路由OK,http://m.wtbis.cn/q/7922
但是想強(qiáng)制自定義的類路由就沒辦法實(shí)現(xiàn)。因?yàn)槲覀冊瓉淼捻?xiàng)目是使用強(qiáng)制路由,就是沒定義路由的方法,寫了也不讓訪問。
目錄結(jié)構(gòu)
app
|--api
|----controller
|------v1
|--------IndexController.php
綁定關(guān)系
/config/plugin/webman/domain/app.php
'bind' => [
'api.test.com' => 'api', // 綁定到blog應(yīng)用
],
使用默認(rèn)路由
/config/route.php
use Webman\Route;
Route::fallback(function () {
return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
訪問http://api.test.com/v1/Index/index.正常.
使用自定義路由 + 默認(rèn)路由
use Webman\Route;
Route::group('/v1', function () {
Route::get('/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});
Route::fallback(function () {
return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
訪問http://api.test.com/v1/Index/index.正常,不沖突,或者是沒有到這里.
使用自定義路由 + 關(guān)閉默認(rèn)路由
use Webman\Route;
Route::group('/v1', function () {
Route::get('/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});
Route::fallback(function () {
return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
Route::disableDefaultRoute();
訪問http://api.test.com/v1/Index/index.異常,404.
使用自定義路由 + 關(guān)閉默認(rèn)路由(去掉分組,用基礎(chǔ)的類路由)
use Webman\Route;
Route::get('/v1/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
Route::fallback(function () {
return response(json_encode(['code' => 404, 'msg' => 'Api Fail']), 404, ['Content-Type' => 'application/json']);
});
Route::disableDefaultRoute();
訪問http://api.test.com/v1/Index/index.異常,404.
另外,多應(yīng)用的能否使用項(xiàng)目路由?因?yàn)閼?yīng)用多了的話,全寫在route.php里面,文件太多了,要改的時(shí)候難找。
搜索了多應(yīng)用+多域名的解決方案,路由失效的解決方案。
這里寫搜到的方案及不適用原因
單獨(dú)的都有,合在一起的沒找到對應(yīng)的解決方案。
不發(fā)下nginx配置?
nginx配置和那文件一模一樣的。。http://m.wtbis.cn/q/7922
if ($host = 'api.test.com') {
rewrite ^/(.*)$ /api/$1 last;
}
這個(gè)nginx配置的含義是自動(dòng)給url的path加一個(gè)前綴api。比如你訪問http://api.test.com/v1/Index/index,nginx會(huì)自動(dòng)將地址變?yōu)閔ttp://api.test.com/api/v1/Index/index并轉(zhuǎn)發(fā)給webman。但是你webman關(guān)閉了自動(dòng)路由,也沒有給/api/v1/Index/index設(shè)置路由,所以404了。我覺得你把
if ($host = 'api.test.com') {
rewrite ^/(.*)$ /api/$1 last;
}
這個(gè)配置刪除了就好了
對的,可以了。api.test.com/abc api.test.com/test 能夠用到域名綁定了
在路由外面多加一層route::group('/api')就可以了。
但是新問題出來了。
Route::group('/api', function () {
Route::group('/v1', function () {
Route::get('/test', [app\api\controller\v1\IndexController::class, 'index']);
});
});
//以上代碼正常。
Route::group('/api', function () {
Route::group('/v1', function () {
Route::get('/index/index', [app\api\controller\v1\IndexController::class, 'index']);
});
});
//這個(gè)和默認(rèn)路由是有沖突的,所以這個(gè)就直接報(bào)404了。
這塊不知道老大的路由思路是什么,開始還以為,Route::disableDefaultRoute()應(yīng)該是個(gè)總開關(guān)。。true,就將所有的默認(rèn)解析全關(guān)了,全用用戶自己的,一個(gè)都沒有定義就全部404。。
false,就混和,默認(rèn)的先定義,用戶自定義的后定義,那么就是后定義的覆蓋以前定義的進(jìn)行處理,最終key是唯一的。
后面發(fā)現(xiàn)這個(gè)Route::disableDefaultRoute()是需要放在路由配置最后面,可能是默認(rèn)和自定義的在兩個(gè)地方?還需要多看看源碼。
感謝大佬,問題解決了。。
我看過源碼,默認(rèn)路由不會(huì)和你的路由有沖突,如果404,說明你地址訪問錯(cuò)了,或者對應(yīng)的路由確實(shí)沒有設(shè)置。
根據(jù)你的上下文來看,估計(jì)是訪問的地址的錯(cuò)了,你給/api/v1/index/index
(注意都是小寫) 設(shè)置了路由,但是可能你去訪問/api/v1/Index/index
了(Index大寫)
用tp習(xí)慣了,訪問的時(shí)候有注意這個(gè)大小寫。api/v1/index/index和api/v1/Index/index,這都是自己去定義的訪問路徑。。然后兩種都試過,都是404.
反正就是用上了域名插件,然后使用多應(yīng)用,再自定義類路由的時(shí)候,關(guān)閉默認(rèn)路由,自定義的路由就不能和以前默認(rèn)的一樣,如果一樣,就404...
這個(gè)試的步驟是這樣的。每一步都先清空路由配置。
1,注釋所有自定義路由,打開默認(rèn)路由。訪問/api/v1/index/index OK
2,自定義/api/v1/aaaa,打開默認(rèn)路由。訪問/api/v1/index/index OK ,訪問/api/v1/aaaa OK
3、自定義/api/v1/aaaa,關(guān)閉默認(rèn)路由。訪問/api/v1/index/index 404,訪問/api/v1/aaaa OK
3、自定義/api/v1/index/index,關(guān)閉默認(rèn)路由。訪問/api/v1/index/index 404
解決了。。不要安裝webman/domain多域名插件,就使用項(xiàng)目默認(rèn)的路由。
然后參照http://m.wtbis.cn/q/7922來配置。
nginx
在nginx配置中,不要按文檔里面的去添加代理。比如我的webman默認(rèn)端口10086.
按文檔的話要在nginx中添加
upstream webman {
server 127.0.0.1:10086;
keepalive 10240;
}
這一段不要加。
就在
server
{
listen 80;
server_name adminapi.test.com api.test.com;
index index.php index.html index.htm default.php default.htm default.html;
root /www/wwwroot/api.test.com/public;
include /www/wwwroot/api.test.com/nginx/*.conf; //加上這一條,代理和rewrite配置都寫在項(xiàng)目文件里。
然后在項(xiàng)目的根目錄下添加nginx目錄,建兩個(gè)文件,一個(gè)文件也行。。一個(gè)代理配置,一個(gè)rewrite配置。
/api.test.com
|--nginx
|----proxy.conf
|----rewrite.conf
proxy.conf
location ^~ /
{
if (!-e $request_filename){
proxy_pass http://127.0.0.1:10086;
}
#設(shè)置域名 不加這個(gè)webman獲取不到用戶IP
proxy_set_header Host $host;
#設(shè)置用戶IP 不加這個(gè)webman獲取不到用戶IP
proxy_set_header X-Real-IP $remote_addr;
#這個(gè)我也不知道干啥的反正加上就對了
proxy_set_header REMOTE-HOST $remote_addr;
#不需要關(guān)閉nginx緩存刪掉下面這行
proxy_cache off;
}
rewrite.conf
if (-f $request_filename){
break;
}
if ($host = 'adminapi.test.com') {
rewrite ^/(.*)$ /adminapi/$1 last;
}
if ($host = 'api.test.com') {
rewrite ^/(.*)$ /api/$1 last;
}
然后這時(shí),/config/route.php里面的路由就跟TP的一樣了,強(qiáng)制路由,子域名綁定。
use Webman\Route;
Route::group('/adminapi', function () {
Route::get('/test', [app\adminapi\controller\v1\IndexController::class, 'index']);
Route::get('/v1/Index/index', [app\adminapi\controller\v1\IndexController::class, 'index']);
});
Route::group('/api', function () {
Route::get('/test', [app\api\controller\v1\IndexController::class, 'index']);
// Route::get('/v1/Index/index', [app\api\controller\v1\IndexController::class, 'index']);
});
Route::disableDefaultRoute();