Rumah > rangka kerja php > Laravel > Laravel5.5仿ThinkPHP的pathinfo传参示例

Laravel5.5仿ThinkPHP的pathinfo传参示例

藏色散人
Lepaskan: 2020-03-28 09:00:32
ke hadapan
2862 orang telah melayarinya

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;
        }
   }
Salin selepas log masuk

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');
Salin selepas log masuk

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[&#39;page&#39;]=3; $array[&#39;keyword&#39;]=&#39;somekeyword&#39;;
     */
    public function path_to_array(Request $request,$path=null){
        $array=array();
        if(strpos($path,&#39;/&#39;)!==false){
            $arr=explode(&#39;/&#39;,$path);
            foreach($arr as $k=>$v){
                if($k>0&&($k%2==0)){
                    $array[$v]=$arr[$k+1];
                }
            }
        }
        if(count($array)>0){
            return $array;
        }
    }
 
}
Salin selepas log masuk

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=&#39;&#39;;
        //当前页
        if(is_array($this->pathinfo)){
            //当前页
            if (array_key_exists("page",$this->pathinfo)) {
                $page=$this->pathinfo[&#39;page&#39;];
            }
            //关键词
            if (array_key_exists("keyword",$this->pathinfo)) {
                $keyword=$this->pathinfo[&#39;keyword&#39;];
            }
        }
        dump($page);
        dump($keyword);
        exit;
}
}
Salin selepas log masuk

推荐:laravel教程

Atas ialah kandungan terperinci Laravel5.5仿ThinkPHP的pathinfo传参示例. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:csdn.net
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan