视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
Laravel5.5仿ThinkPHP的pathinfo传参示例
2020-11-03 18:15:27 责编:小采
文档

PS:目前所能找到的方法,希望不会被大佬们鄙视...

1、我的nginx配置:

#test6 admin
 server {
 listen 80;
 server_name admin.test6.local;
 root "d:/data/www/test6/public/admin";
 index index.html index.htm index.php;
 error_page 404 /404.html;
 location = /404.html {
 return 404 'Sorry, File not Found!';
 }
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html; # windows dir
 }
location / {
 try_files $uri $uri/ /index.php?$query_string;
}
 
 location ~ /Uploads/.*.php$ {
 deny all;
 }
 location ~ .php/ {
 if ($request_uri ~ ^(.+.php)(/.+?)($|?)) { }
 fastcgi_pass 127.0.0.1:9000;
 include fastcgi_params;
 fastcgi_param SCRIPT_NAME $1;
 fastcgi_param PATH_INFO $2;
 fastcgi_param SCRIPT_FILENAME $document_root$1;
 }
 location ~ .php$ {
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
 location ~ /.ht {
 deny all;
 }
 }

2、路由 /routes/admin.php

PS:目前用的方法是这样的

Route::get('/shop/goods_list', 'ShopGoodsController@goods_list');
Route::get('/shop/goods_list/page/{page}/limit/{limit}', 'ShopGoodsController@goods_list');
Route::get('/shop/goods_list/page/{page}/limit/{limit}/keyword/{keyword}', 'ShopGoodsController@goods_list');

3、基类 appHttpControllersAdminPubliBaseController.php

PS:请别吐槽那个Publi,我也很无奈啊..

<?php
 
namespace AppHttpControllersAdminPubli;
 
use IlluminateFoundationBusDispatchesJobs;
use IlluminateRoutingController;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateFoundationAuthAccessAuthorizesRequests;
use IlluminateHttpRequest;
class BaseController extends Controller
{
 use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
 
 //构造函数
 public function __construct() {
 $this->middleware(function ($request, $next) {
 //未登录的处理
 //$this->dosomething($request);
 //仿TP用pathinfo传参
 $path=$request->path();
 $this->pathinfo=$this->path_to_array($request,$path);
 return $next($request);
 });
 }
 
 /*路径转数组
 *输入路径,如:http://admin.test6.local/shop/goods_list/page/3
 * 返回数组,如:$array['page']=3; $array['keyword']='somekeyword';
 */
 public function path_to_array(Request $request,$path=null){
 $array=array();
 if(strpos($path,'/')!==false){
 $arr=explode('/',$path);
 foreach($arr as $k=>$v){
 if($k>0&&($k%2==0)){
 $array[$v]=$arr[$k+1];
 }
 }
 }
 if(count($array)>0){
 return $array;
 }
 }
 
}

3、控制器 appHttpControllersAdminShopGoodsController.php

namespace AppHttpControllersAdminShop;
use AppHttpControllersAdminPubliBaseController;
use IlluminateHttpRequest;
class GoodsController extends BaseController {
public function goods_list(Request $request){
//获取参数
 $page=1;
 $keyword='';
 //当前页
 if(is_array($this->pathinfo)){
 //当前页
 if (array_key_exists("page",$this->pathinfo)) {
 $page=$this->pathinfo['page'];
 }
 //关键词
 if (array_key_exists("keyword",$this->pathinfo)) {
 $keyword=$this->pathinfo['keyword'];
 }
 }
 dump($page);
 dump($keyword);
 exit;
}
}

推荐:laravel教程

下载本文
显示全文
专题