简体   繁体   English

用2个或更多参数重写URL

[英]Rewrite url with 2 or more params

I asked a question like this before but since i still can't find an answer to this i'll ask it again :-s. 我之前曾问过一个这样的问题,但是由于我仍然找不到答案,因此我将再次提出它:-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');
}

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 并且我使用此htaccess文件将我的网址从/base/index.php?page=somepage重写为/base/somepage (somepage是我的模板文件夹中的.php文件)

RewriteEngine On

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

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 . 使用1参数可以正常工作,但是我的问题是我不知道如何重写第二个参数/base/profile?user=username (没有htaccess文件,它看起来像是/base/index.php?page=profile?user=username ),我希望它看起来像这样/base/profile/username

I hope that this question is understandable :-s 我希望这个问题是可以理解的:-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. 我还有很多东西要学习,所以我会尝试通过我所做的事情来解释,但这可能是完全可以改进的。

Today's PHP Standard Recommendation about routing and interpreting request should implement PSR7 . 今天有关路由和解释请求的PHP标准建议书应实现PSR7

I personnaly use it through a FrontController Design Pattern in a MVC framework I'm building to understand these concepts. 我通过正在构建的MVC框架中的FrontController设计模式亲自使用它来理解这些概念。 My folders are organized like this : 我的文件夹是这样组织的:

  • Public : 上市 :
    Where I launch my web server, where you can find JavaScript/CSS. 在启动Web服务器的位置,您可以找到JavaScript / CSS。 There's an index.php which just contains 有一个index.php仅包含

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

  • App : Where there's the router and mostly all generic code 应用程序:哪里有路由器,几乎所有的通用代码

  • Src : Where there's the specific code to the app. Src:哪里有应用程序的特定代码。 That means controllers and entities for now. 这意味着现在是控制器和实体。

  • Vendor : Composer dependencies such as GuzzleHTTP to have a class between the actual request and the code. 供应商:Composer依赖项(例如GuzzleHTTP)在实际请求和代码之间具有一个类。

Here's the code in my root's index.php : 这是我根目录下的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());

The main point of it is that I interpret the request within an instance of a class implementing PSR7. 重点是我在实现PSR7的类的实例中解释请求。

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. 在我的FrontController中,我的请求通过某些方法(例如删除尾部斜杠)传递,最终最终在要处理的Router类中发送

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 : 我的Router类的目的是检查URI是否存在于我将所有以此格式存储在json文件中的所有路由的数组中:

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

This is where I use regex to match variable inside the URI (/article/:id for example) too. 这也是我使用正则表达式在URI(例如/ article /:id)中匹配变量的地方。
This Class can be resumed as "Does this URI exists in my app?". 可以按“此URI是否在我的应用程序中存在吗?”的形式恢复此类。

At this point, I instantiate a new Route class with all the array as parameter. 此时,我将所有数组作为参数实例化一个新的Route类。 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 ?" 从这里开始,我必须回答诸如“是否将它附加到控制器中的方法上?”,“是否处理了所要求的方法?”之类的问题。 ... ...

To summarize, at this point, I have an Instance of a Class that represents the Request, another one that represents all my routes. 总而言之,在这一点上,我有一个代表该请求的Class实例,另一个代表所有的路线。 I confront them to get ONE Route which I'm gonna manipulate through an Instance of a Class Route. 我面对他们以获得一个我将通过类路线实例操作的路线。

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. 如果它通过了所有这些测试,那么我可以实例化正确的Controller,在该控制器中将存在应用程序特定的逻辑部分,需要执行一些操作以获取数据,然后将这些数据发送到视图中以生成HTML输出,然后将其发送一直返回到我的函数send,以便在您请求特定的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... 它迫使您分离应用程序的所有捆绑包:控制器既不是模型也不是路由器...

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

Hope it was understandable and helpful 希望它是可以理解和有用的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM