怎样解析URL中的多个动态参数名并获取相应参数值?

kenny534

控制器需要接收两个参数(除$request以外),多应用下,控制器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
});
问题:怎样实现第3种访问需求?

1150 2 0
2个回答

wolfcode
      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);
      });
  • kenny534 2022-04-07

    这个方法可以获取到数据,但引起了一个新的问题:
    在more方法里调用 return view('index/mytemplate,['p1'=>$data1,'p2'=>$data2]);时报错说Exception: template not exists
    说是 /app/view/index/mytemplate.html文件不存在(貌似这是单应用模式下的模板路径),而真正的模板文件是存放在
    /app/index/view/index/mytemplate.html (这是我多应用下保存的模板文件)。感觉像是“串线了“ ? 烦请指教,谢谢:)

  • wolfcode 2022-04-07

    /app/view/下创建对应应用的文件夹
    比如index应用
    则路径: /app/view/index/index/mytemplate.html

  • wolfcode 2022-04-07

    return view('index/index/mytemplate,['p1'=>$data1,'p2'=>$data2]);

  • liziyu 2022-04-07

    高手,这是高手!

  • kenny534 2022-04-07

    我把问题整理了一下,您再给看看?

kenny534
<?php    //文件目录:app\index\controller\Community.php
namespace app\index\controller;

use app\BaseController;
use support\Request;
class Community extends BaseController{
     //index方法未在route.php中做配置,可以正常找到模板文件并显示内容
     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]);
     }
}

报错内容:

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\ 为什么经路由配置之后,topic.html模板就找不到了?而没有配置路由的index.html就能找得到?
问题2:按报错提示,将文件拷贝至:app\view\community\topic.html ,就可以显示了,这应该是把模板放到公共目录保存了。那么怎样实现模板的分应用存放呢?
问题3:想同时实现如下访问:
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 / 路由配置已解决/
有什么其它方法既可以实现more方法灵活传入$status 和 $order 两个参数的值,又不影响模板读取目录呢?
问题4:路由配置中的闭包写法,是否修改了webman的默认模板路径?如果是,那怎么设置该路径?

  • 暂无评论
年代过于久远,无法发表回答
×
🔝