控制器需要接收兩個參數(shù)(除$request以外),多應(yīng)用下,控制器index的more方法如下:
public function more(Request $request,$status='',$order= ''){
}
由以下四種訪問需求:
1、index/index/more
2、index/index/more/status/no/39
3、index/index/more/order/3
4、index/index/more/status/no/39/order/3
其中:
1、 可以不需要配置路由
2、4 路由配置如下:
Route::group('/index/index',function (){
Route::any("/more/status/{no}",[app\index\controller\Index::class,'more']); //2
Route::any('/more/status/{no}/order/{order}',[app\index\controller\Index::class,'more']); //4
});
問題:怎樣實現(xiàn)第3種訪問需求?
Route::any("/status/{status}", function ($request, $status) {
return (new \app\controller\Index())->more($request, $status, '');
});
Route::any("/order/{order}", function ($request, $order) {
return (new \app\controller\Index())->more($request, '', $order);
});
這個方法可以獲取到數(shù)據(jù),但引起了一個新的問題:
在more方法里調(diào)用 return view('index/mytemplate,['p1'=>$data1,'p2'=>$data2]);時報錯說Exception: template not exists
說是 /app/view/index/mytemplate.html文件不存在(貌似這是單應(yīng)用模式下的模板路徑),而真正的模板文件是存放在
/app/index/view/index/mytemplate.html (這是我多應(yīng)用下保存的模板文件)。感覺像是“串線了“ ? 煩請指教,謝謝:)
/app/view/下創(chuàng)建對應(yīng)應(yīng)用的文件夾
比如index應(yīng)用
則路徑: /app/view/index/index/mytemplate.html
<?php //文件目錄:app\index\controller\Community.php
namespace app\index\controller;
use app\BaseController;
use support\Request;
class Community extends BaseController{
//index方法未在route.php中做配置,可以正常找到模板文件并顯示內(nèi)容
public function index(Request $request){
.......
// url:http://centos85.cn:8787/index/community/
return view('community/index',['p1'=$data1,'p2'=>$data2]);
//模板文件所在目錄:app\index\view\community\index.html
}
//more方法在route.php中配置如下
Route::group('/index/community',function (){
Route::any("/more[/]",function ($request){
return (new \app\index\controller\Community() )->more($request,'','');
});
Route::any("/more/status/{no}",function ($request,$no){
return (new \app\index\controller\Community() )->more($request,$no,'');
});
Route::any("/more/order/{order}",function ($request,$order){
return (new \app\index\controller\Community() )->more($request,'',$order);
});
Route::any('/more/status/{no}/order/{order}',[app\index\controller\Community::class,'more']);
});
public function more(Request $request,$status,$order){
…………
//該語句報錯:Exception: template not exists:/home/kenny/php_study/webman134/app/view/community/topic.html
return view('community/topic',['p1'=$data1,'p2'=>$data2]);
}
}
報錯內(nèi)容:
Exception: template not exists:/home/kenny/php_study/webman134/app/view/community/topic.html in /home/kenny/php_study/webman134/vendor/topthink/think-template/src/Template.php:1255
Stack trace:
#0 /home/kenny/php_study/webman134/vendor/topthink/think-template/src/Template.php(235): think\Template->parseTemplateFile()
#1 /home/kenny/php_study/webman134/vendor/workerman/webman-framework/src/support/view/ThinkPHP.php(59): think\Template->fetch()
#2 /home/kenny/php_study/webman134/support/helpers.php(165): support\view\ThinkPHP::render()
#3 /home/kenny/php_study/webman134/app/index/controller/Community.php(1020): view()
#4 /home/kenny/php_study/webman134/config/route.php(19): app\index\controller\Community->more()
#5 /home/kenny/php_study/webman134/vendor/workerman/webman-framework/src/App.php(253): Webman\Route::{closure}()
#6 /home/kenny/php_study/webman134/vendor/webman/action-hook/src/ActionHook.php(39): Webman\App::Webman\{closure}()
#7 /home/kenny/php_study/webman134/vendor/workerman/webman-framework/src/App.php(245): Webman\ActionHook\ActionHook->process()
#8 /home/kenny/php_study/webman134/vendor/workerman/webman-framework/src/App.php(330): Webman\App::Webman\{closure}()
#9 /home/kenny/php_study/webman134/vendor/workerman/webman-framework/src/App.php(147): Webman\App::findRoute()
#10 /home/kenny/php_study/webman134/vendor/workerman/workerman/Connection/TcpConnection.php(638): Webman\App->onMessage()
#11 /home/kenny/php_study/webman134/vendor/workerman/workerman/Events/Select.php(295): Workerman\Connection\TcpConnection->baseRead()
#12 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(2431): Workerman\Events\Select->loop()
#13 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(1555): Workerman\Worker->run()
#14 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(1397): Workerman\Worker::forkOneWorkerForLinux()
#15 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(1371): Workerman\Worker::forkWorkersForLinux()
#16 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(1692): Workerman\Worker::forkWorkers()
#17 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(1641): Workerman\Worker::monitorWorkersForLinux()
#18 /home/kenny/php_study/webman134/vendor/workerman/workerman/Worker.php(551): Workerman\Worker::monitorWorkers()
#19 /home/kenny/php_study/webman134/start.php(108): Workerman\Worker::runAll()
#20 {main}
問題1:兩個模板文件:index.html, topic.html 均在 app\index\view\community\ 為什么經(jīng)路由配置之后,topic.html模板就找不到了?而沒有配置路由的index.html就能找得到?
問題2:按報錯提示,將文件拷貝至:app\view\community\topic.html ,就可以顯示了,這應(yīng)該是把模板放到公共目錄保存了。那么怎樣實現(xiàn)模板的分應(yīng)用存放呢?
問題3:想同時實現(xiàn)如下訪問:
1、index/community/more / 路由配置已解決/
2、index/community/more/status/no/39
3、index/community/more/order/3
4、index/community/more/status/no/39/order/3 / 路由配置已解決/
有什么其它方法既可以實現(xiàn)more方法靈活傳入$status 和 $order 兩個參數(shù)的值,又不影響模板讀取目錄呢?
問題4:路由配置中的閉包寫法,是否修改了webman的默認(rèn)模板路徑?如果是,那怎么設(shè)置該路徑?