用2个或更多参数重写url [英] Rewrite url with 2 or more params

查看:30
本文介绍了用2个或更多参数重写url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前曾问过这样的问题,但是由于我仍然找不到答案,所以我会再次问它:-s.

I asked a question like this before but since i still can't find an answer to this i'll ask it again :-s.

我正在使用这个非常基本的模板"脚本:

I'm using this very basic 'templating' script:

require_once 'core/init.php';

if(empty($_GET['page'])){
    header('Location: home');
    die();
}

$basePath = dirname(__FILE__) . '/';
$viewPath = $basePath . 'view/';

$view = scandir($viewPath);

foreach($view as $file)     
{
    if (!is_dir($viewPath . $file))
    {
        $pages[] = substr($file, 0, strpos($file, '.'));

    }
}

if(in_array($_GET['page'], $pages)){
    include($viewPath . $_GET['page'] . '.php');
} else{
    include($basePath . '404.php');
}

,并且我正在使用此htaccess文件将URL从/base/index.php?page=somepage重写为/base/somepage(某页是我的模板文件夹中的.php文件)

and i'm rewriting my url from /base/index.php?page=somepage to /base/somepage(somepage is a .php file in my template folder) using this htaccess file

RewriteEngine On

RewriteBase /base/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?page=$1 [L,QSA]

具有1个参数,它工作得很好,但是我的问题是我不知道如何重写第二个参数/base/profile?user=username(没有htaccess文件,它看起来像这样的/base/index.php?page=profile?user=username),我希望它看起来就像这样/base/profile/username.

With 1 parameter it works just fine but my problem is that i don't know how to rewrite a second param /base/profile?user=username (with no htaccess file this would have look like this /base/index.php?page=profile?user=username) and i want it to look like this /base/profile/username.

我希望这个问题是可以理解的:-s

I hope that this question is understandable :-s

推荐答案

路由是一个实际问题,我无法在一个评论中穷尽,但我会尽力而为.请原谅我的英语水平,如果您听不懂,请告诉我.我还有很多东西要学习,所以我会尝试通过我所做的事情来解释,但这可能是完全可改进的.

Routing is a real issue and I can't be exhaustive in one comment, but I'll try to do my best. Please forgive my aproximative english and let me know if you don't understand. I still have a lot to learn so I'll try to explain through something I made, but it is probably fully improvable.

今天有关路由和解释请求的PHP标准建议书应实现 PSR7

Today's PHP Standard Recommendation about routing and interpreting request should implement PSR7.

我个人通过我正在构建的MVC框架中的 FrontController设计模式使用它了解这些概念.我的文件夹是这样组织的:

I personnaly use it through a FrontController Design Pattern in a MVC framework I'm building to understand these concepts. My folders are organized like this :

  • 公共:
    在我启动Web服务器的地方,您可以在其中找到JavaScript/CSS.有一个index.php仅包含

  • Public :
    Where I launch my web server, where you can find JavaScript/CSS. There's an index.php which just contains

require_once('../index.php');

require_once('../index.php');

App: 在哪里有路由器以及几乎所有的通用代码

App : Where there's the router and mostly all generic code

Src: 该应用有特定代码的地方.这意味着现在是控制器和实体.

Src : Where there's the specific code to the app. That means controllers and entities for now.

供应商: 诸如 GuzzleHTTP 之类的Composer依赖项在实际请求和代码之间有一个类.

Vendor : Composer dependencies such as GuzzleHTTP to have a class between the actual request and the code.

这是我根目录下的index.php中的代码:

Here's the code in my root's index.php :

<?php
require_once 'vendor/autoload.php';

use Namespace\FrontController;
use \GuzzleHttp\Psr7\ServerRequest;
use function Http\Response\send;

$front_ctrl = new FrontController(ServerRequest::fromGlobals());
send($front_ctrl->getResponse());

主要要点是我在实现PSR7的类的实例中解释请求.

The main point of it is that I interpret the request within an instance of a class implementing PSR7.

在我的FrontController中,我的请求通过某些方法(例如删除尾部斜杠)进行传送,最终发送在要处理的Router类中.

In my FrontController, my request travel through some methods (such as removing trailing slash) to finnaly be sent in a Router class to be handled.

我的Router类的目的是检查 URI 存在于将所有这种格式的所有路径存储在json文件中的数组中:

The purpouse of my Router class is to check if the URI exist in the array where I stocked all my routes under this format in a json file :

{
    "/": [
      "AppController",
      "indexAction",
      ["GET", "POST"]
    ],...
}

这也是我使用正则表达式在URI(例如/article/:id)中匹配变量的地方.
可以按我的应用程序中是否存在此URI?"的形式恢复该类.

This is where I use regex to match variable inside the URI (/article/:id for example) too.
This Class can be resumed as "Does this URI exists in my app?".

在这一点上,我实例化了一个新的Route类,并将所有数组作为参数.从这里开始,我必须回答诸如是否将它附加到控制器中的方法上?",是否处理了所要求的方法?"之类的问题. ...

At this point, I instantiate a new Route class with all the array as parameter. From here, I have to answer questions such as "Is it attached to a method in a controller ?", "Does the method in which it is asked is handled ?" ...

总而言之,在这一点上,我有一个代表该请求的Class实例,另一个代表了我的路线所有的实例.我面对他们以获得一个路线,该路线将通过类路线的实例进行操纵.

To summarize, at this point, I have an Instance of a Class that represents the Request, another one that represents all my routes. I confront them to get ONE Route which I'm gonna manipulate through an Instance of a Class Route.

如果它通过了所有这些测试,那么我可以实例化正确的Controller,在该控制器中将存在应用程序特定的逻辑部分,需要采取一些措施来获取数据,然后将其发送到视图中以生成HTML输出,我将一路发送回我的函数send,以便在您请求特定的URI时显示输出.

If it passed all those tests, then I can instantiate the right Controller, where there will be the logical part specific of the app, requiring some action to get data, that I will send in my views to generate a HTML output which I will send back all the way back to my function send so the output is displayed when you ask for a specific URI.

这个长答案的重点是向您展示几乎完全独立于服务器的内容.如果您的应用程序越来越大并且必须处理更特定的路由规则,这也很有用.它迫使您分离应用程序的所有捆绑包:控制器既不是模型,也不是路由器...

The main point of this long answer is to show you something that is almost completely independent from the server. It's also useful if your app gets bigger and has to handle more specific rules for routing. It forces you to separates all the bundles of your app : A Controller is not a Model neither a Router...

尝试找到一些很好的教程来学习PHP中的面向对象编程,这可以避免简单的安全问题,并在开发应用程序时为您带来更多的舒适感:)

Try to find some good tutorials to learn Oriented Object Programmation in PHP, which would avoid easy security issues and give you much more comfort when developping an app :)

希望这是可以理解且有用的

Hope it was understandable and helpful

这篇关于用2个或更多参数重写url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆