您好,登錄后才能下訂單哦!
這篇文章主要介紹Laravel5.5仿ThinkPHP中pathinfo傳參的案例,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
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', 'Shop\GoodsController@goods_list'); Route::get('/shop/goods_list/page/{page}/limit/{limit}', 'Shop\GoodsController@goods_list'); Route::get('/shop/goods_list/page/{page}/limit/{limit}/keyword/{keyword}', 'Shop\GoodsController@goods_list');
3、基類 \app\Http\Controllers\Admin\Publi\BaseController.php
PS:請別吐槽那個Publi,我也很無奈啊..
<?php namespace App\Http\Controllers\Admin\Publi; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Routing\Controller; use Illuminate\Foundation\Validation\ValidatesRequests; use Illuminate\Foundation\Auth\Access\AuthorizesRequests; use Illuminate\Http\Request; 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、控制器 \app\Http\Controllers\Admin\Shop\GoodsController.php
namespace App\Http\Controllers\Admin\Shop; use App\Http\Controllers\Admin\Publi\BaseController; use Illuminate\Http\Request; 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; } }
以上是“Laravel5.5仿ThinkPHP中pathinfo傳參的案例”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。