简体   繁体   English

如何避免在 php 中的 URL 中重复路径?

[英]How to avoid the repeating of a path in a URL in php?

I have an URL path like我有一个像

http://localhost:8080/laravel/api/data/create/

Where /laravel/api is the path from the config file其中/laravel/api是配置文件的路径

and /data/create/ is the path which I used in traits . /data/create/是我在traits使用的路径。

The problem is everytime when I run the function named connection() repeatedly,问题是每次我重复运行名为connection()的函数时,

In Tinker, If I do在 Tinker,如果我这样做

$data = new Sample;
$data->connection();
http://localhost:8080/laravel/api/data/create/
$data->connection();
http://localhost:8080/laravel/api/data/create/data/create/
$data->connection();
http://localhost:8080/laravel/api/data/create/data/create/data/create/

The path /data/create/ is repeated every time when I perform connection().每次执行 connection() 时都会重复路径/data/create/

It should not be like that, only once the path should be appended, if we perform connection(), like the below.不应该这样,如果我们执行connection(),则只应附加一次路径,如下所示。

$data->connection();
http://localhost:8080/laravel/api/data/create/

I am using a method chaining for appending path .我正在使用一种方法链接来追加 path 。

protected $resource[]; //is declared already in the code
protected $url[];
public function addPath()
{
        $old_uri = explode('/', $this->resource['path']);
        $add_uri = explode('/', $this->path);
        $new_uri = array_merge($old_uri, $add_uri);
        $this->resource['path'] = '/'.implode('/', array_filter($new_uri));
        return $this;
    
   }
public function connection()
{
   $this->addPath()->unparse_url();
   return $url;
}
private function unparse_url()
    {
        $scheme = isset($this->resource['scheme']) ? $this->resource['scheme'].'://' : '';
        $host = isset($this->resource['host']) ? $this->resource['host'] : '';
        $port = isset($this->resource['port']) ? ':'.$this->resource['port'] : '';
        $user = isset($this->resource['user']) ? $this->resource['user'] : '';
        $pass = isset($this->resource['pass']) ? ':'.$this->resource['pass'] : '';
        $pass = ($user || $pass) ? "$pass@" : '';
        $path = isset($this->resource['path']) ? $this->resource['path'] : '';
        $query = isset($this->resource['query']) ? '?'.$this->resource['query'] : '';
        $fragment = isset($this->resource['fragment']) ? '#'.$this->resource['fragment'] : '';

        $this->url = "$scheme$user$pass$host$port$path$query$fragment";

        return $this;
    }

Could someone please help to fix the issue?有人可以帮忙解决这个问题吗? Thanks.谢谢。

If we disregard from your undefined variables, the issue is that your code appends the path every time you call the connection() -method since it calls the addPath() -method.如果我们忽略您未定义的变量,问题是您的代码每次调用connection() -方法时都会附加路径,因为它调用addPath() -方法。

If you only want it to append it once, there are some alternatives.如果您只希望它附加一次,则有一些替代方法。

Alternative 1备选方案 1

If you always want the path to be added, you can simple call the addPath() -method from the constructor instead.如果您总是希望添加路径,您可以简单地从构造函数调用addPath()方法。

public function __construct()
{
    $this->addPath()->unparse_url();
}

and just have the connection() -method like this:并且只有这样的connection()方法:

public function connection()
{
    return $this->url;
}

This would work well as long as you don't need to use the original values of $this->url and $this->resource['path'] for anything since they will be changed when the class is instantiated.只要您不需要将$this->url$this->resource['path']的原始值用于任何事情,这将很有效,因为它们将在类实例化时更改。

Alternative 2备选方案 2

You can set class property as a flag, saying if the path already been added or not.您可以将类属性设置为标志,说明是否已添加路径。 Then you simply check that flag in your addPath() -method.然后您只需在addPath()方法中检查该标志。

// Default value is false, since we haven't added it yet
protected $pathAdded = false;

public function addPath()
{
    if ($this->pathAdded === false) {
        // It's still false so it hasn't been added yet
        
        $old_uri = explode('/', $this->resource['path']);
        $add_uri = explode('/', $this->path);
        $new_uri = array_merge($old_uri, $add_uri);
        $this->resource['path'] = '/'.implode('/', array_filter($new_uri));

        // Set the flag to true so we know it's already been done
        $this->pathAdded = true;
    }

    return $this;
}

This way, it will only append the path on the first call to that method.这样,它只会在第一次调用该方法时附加路径。

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

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