Home PHP Framework Laravel Laravel5.5 imitates ThinkPHP's pathinfo parameter passing example

Laravel5.5 imitates ThinkPHP's pathinfo parameter passing example

Mar 28, 2020 am 09:00 AM

Laravel5.5 imitates ThinkPHP's pathinfo parameter passing example

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

1、我的nginx配置:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

#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;

        }

   }

Copy after login

2、路由 /routes/admin.php

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

1

2

3

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');

Copy after login

3、基类 \app\Http\Controllers\Admin\Publi\BaseController.php

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

<?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;

        }

    }

  

}

Copy after login

3、控制器 \app\Http\Controllers\Admin\Shop\GoodsController.php

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

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;

}

}

Copy after login

推荐:laravel教程

The above is the detailed content of Laravel5.5 imitates ThinkPHP's pathinfo parameter passing example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Build a RESTful API with Advanced Features in Laravel? How to Build a RESTful API with Advanced Features in Laravel? Mar 11, 2025 pm 04:13 PM

This article guides building robust Laravel RESTful APIs. It covers project setup, resource management, database interactions, serialization, authentication, authorization, testing, and crucial security best practices. Addressing scalability chall

How do I use Laravel's components to create reusable UI elements? How do I use Laravel's components to create reusable UI elements? Mar 17, 2025 pm 02:47 PM

The article discusses creating and customizing reusable UI elements in Laravel using components, offering best practices for organization and suggesting enhancing packages.

How to Implement OAuth2 Authentication and Authorization in Laravel? How to Implement OAuth2 Authentication and Authorization in Laravel? Mar 12, 2025 pm 05:56 PM

This article details implementing OAuth 2.0 authentication and authorization in Laravel. It covers using packages like league/oauth2-server or provider-specific solutions, emphasizing database setup, client registration, authorization server configu

What Are the Best Practices for Using Laravel in a Cloud-Native Environment? What Are the Best Practices for Using Laravel in a Cloud-Native Environment? Mar 14, 2025 pm 01:44 PM

The article discusses best practices for deploying Laravel in cloud-native environments, focusing on scalability, reliability, and security. Key issues include containerization, microservices, stateless design, and optimization strategies.

How do I create and use custom Blade directives in Laravel? How do I create and use custom Blade directives in Laravel? Mar 17, 2025 pm 02:50 PM

The article discusses creating and using custom Blade directives in Laravel to enhance templating. It covers defining directives, using them in templates, and managing them in large projects, highlighting benefits like improved code reusability and r

How can I create and use custom validation rules in Laravel? How can I create and use custom validation rules in Laravel? Mar 17, 2025 pm 02:38 PM

The article discusses creating and using custom validation rules in Laravel, offering steps to define and implement them. It highlights benefits like reusability and specificity, and provides methods to extend Laravel's validation system.

What Are the Best Ways to Handle File Uploads and Cloud Storage in Laravel? What Are the Best Ways to Handle File Uploads and Cloud Storage in Laravel? Mar 12, 2025 pm 05:54 PM

This article explores optimal file upload and cloud storage strategies in Laravel. It examines local storage vs. cloud providers (AWS S3, Google Cloud, Azure, DigitalOcean), emphasizing security (validation, sanitization, HTTPS) and performance opti

How do I use Laravel's Artisan console to automate common tasks? How do I use Laravel's Artisan console to automate common tasks? Mar 17, 2025 pm 02:39 PM

Laravel's Artisan console automates tasks like generating code, running migrations, and scheduling. Key commands include make:controller, migrate, and db:seed. Custom commands can be created for specific needs, enhancing workflow efficiency.Character

See all articles