PHP - 封装 Url 操作工具类

1. 直接上代码【本教程封装在 laravel , 但不局限于 Laravel 使用 】

文件位置: app\Handlers\LearnkuUrlHandler.php

<?php
/**
 * Url 解析工具类
 * 用户实例:
 *  $learnkuUrl = new LearnkuUrlHandler(url()->full());
 *      $learnkuUrl->show('');
 *      $learnkuUrl->update([]);
 *      $learnkuUrl->delete([]);
 *      $learnkuUrl->changePath('');
 */

namespace App\Handlers;

class LearnkuUrlHandler
{
    protected $domain = '';
    protected $url = [];
    protected $params = [];

    /**
     *
     * LearnkuUrlHandler constructor.
     * @param string $url http://learnku.net/xxx.html?a=1&b=2
     */
    public function __construct($url)
    {
        // ['query' => 'a=1&b=2']
        $this->url = parse_url($url);

        // http://learnku.net/xxx.html?
        $this->domain = $this->url['path'];

        // [ a => 1, b => 2 ]
        if (isset($this->url['query'])) {
            parse_str($this->url['query'], $this->params);
        }
    }

    /**
     * 获取单个参数
     * @param string $key
     * @return string
     */
    public function show($key)
    {
        if (array_key_exists($key, $this->params)) {
            return $this->params[$key];
        } else {
            return '';
        }
    }

    /**
     * 修改参数 返回完整 url
     * @param array $item [
     *      'a' => 100,
     *      'b' => 200,
     * ]
     * @return string
     */
    public function update($item = [])
    {
        $this->params = array_merge($this->params, $item);
        return $this->_getUrl();
    }

    /**
     * 删除参数 返回完整 url
     * @param array $item
     * @return string
     */
    public function delete($item = [])
    {
        $this->params = array_diff_key($this->params, $item);
        return $this->_getUrl();
    }

    /**
     * 改变 域名
     * @param $domain
     * @return string
     */
    public function changePath($domain)
    {
        $this->domain = $domain;
        return $this->_getUrl();
    }

    /**
     * 获取 完整 url => http://www.leanku.net/xxx.html?a=1&b=2
     * @return string
     */
    protected function _getUrl()
    {
        return $this->domain . '?' . http_build_query($this->params);
    }

    /**
     * 使用 http_build_query 代替
     * 获取 url 参数 【 使用 http_build_query 代替 】
     * @param $array_query
     * @return string => a=1&b=2
     */
    protected function _getUrlQuery($array_query)
    {
        $tmp = array();
        foreach($array_query as $k=>$param)
        {
            $tmp[] = $k.'='.$param;
        }
        $params = implode('&',$tmp);
        return $params;
    }

    /**
     * 使用 parse_str 代替
     * 反转 url 参数为 params 数组 【 使用 parse_str 代替 】
     * @param $query
     * @return array
     */
    protected function _convertUrlQuery($query)
    {
        $queryParts = explode('&', $query);
        $params = array();
        foreach ($queryParts as $param)
        {
            $item = explode('=', $param);
            $params[$item[0]] = $item[1];
        }
        return $params;
    }
}

2. 用法实例

App\Http\Controllers\SearchController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Handlers\LearnkuUrlHandler;

class SearchController extends Controller {
    public function index(Request $request){
        $test_url = "http://learnku.test/search?q=a&order=created_at_asc&page=1";
        // $learnkuUrl = new LearnkuUrlHandler(url()->full());
        $learnkuUrl = new LearnkuUrlHandler( $test_url );

        dump($learnkuUrl->show('order'));
        // "created_at_asc"

        dump($learnkuUrl->update([
            'order'=> 'created_at_desc'
        ]));
        // "http://learnku.test/search?order=created_at_desc&page=1&q=a"

        dump($learnkuUrl->delete([
            'order'=> 'created_at_desc'
        ]));
        // "http://learnku.test/search?page=1&q=a"

        dd('....');
    }
}
讨论数量: 0

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!