Переименование URL моего сайта PHP с помощью htaccess - PullRequest
0 голосов
/ 29 января 2019

Я хотел бы переименовать URL-адреса, имеющиеся на моем веб-сайте, с помощью файла .htaccess: от http://siteaddress.com/city.php?city=london до http://siteaddress.com/city/city-london

таким же образом, например, для переименования других городов;../city.php?city=birmingham...

1 Ответ

0 голосов
/ 29 января 2019

Я использовал шаблон архитектуры MVC для этого.Вот несколько стартовых ресурсов, которые помогут вам найти нужное место.Возможно, есть и лучшие способы, но это один из способов.

Стартовый код: panique / mini

основной алгоритм маршрутизации

Ниже приводится описание алгоритма.Файл htaccess в папке Public.

# If the following conditions are true, then rewrite the URL:
# If the requested filename is not a directory,
RewriteCond %{REQUEST_FILENAME} !-d
# and if the requested filename is not a regular file that exists,
RewriteCond %{REQUEST_FILENAME} !-f
# and if the requested filename is not a symbolic link,
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

Маршруты были обработаны в Application.php с использованием следующего алгоритма

<?php
class Application
{
    /** @var null The controller */
    private $url_controller = null;
    /** @var null The method (of the above controller), often also named "action" */
    private $url_action = null;
    /** @var array URL parameters */
    private $url_params = array();

    /**
     * "Start" the application:
     * Analyze the URL elements and calls the according controller/method or the fallback
     */
    public function __construct()
    {
        // create array with URL parts in $url
        $this->splitUrl();
        // check for controller: no controller given ? then load start-page
        if (!$this->url_controller) {
            require APP . 'controller/home.php';
            $page = new Home();
            $page->index();
        } 
        elseif (file_exists(APP . 'controller/' . $this->url_controller . '.php')) {
            // here we did check for controller: does such a controller exist ?
            // if so, then load this file and create this controller
            // example: if controller would be "city", then this line would translate into: $this->city= new City();
            require APP . 'controller/' . $this->url_controller . '.php';
            $this->url_controller = new $this->url_controller();
            // check for method: does such a method exist in the controller ?
            if (method_exists($this->url_controller, $this->url_action)) {
                if (!empty($this->url_params)) {
                    // Call the method and pass arguments to it
                    call_user_func_array(array($this->url_controller, $this->url_action), $this->url_params);
                } else {
                    // If no parameters are given, just call the method without parameters, like $this->city->method();
                    $this->url_controller->{$this->url_action}();
                }
            } else {
                if (strlen($this->url_action) == 0) {
                    // no action defined: call the default index() method of a selected controller
                    $this->url_controller->index();
                }
                else {
                    header('location: ' . URL . 'problem');
                }
            }
        } else {
            header('location: ' . URL . 'problem');
        }
    }

    private function splitUrl()
    {
        if (isset($_GET['url'])) {
            // split URL
            $url = trim($_GET['url'], '/');
            $url = filter_var($url, FILTER_SANITIZE_URL);
            $url = explode('/', $url);

            // Put URL parts into according properties
            $this->url_controller = isset($url[0]) ? $url[0] : null;
            $this->url_action = isset($url[1]) ? $url[1] : null;

           // Remove controller and action from the split URL
            unset($url[0], $url[1]);

            // Rebase array keys and store the URL params
            $this->url_params = array_values($url);
        }
    }

, если ваши URL-адреса похожи на приведенные ниже, они будут нарушенывниз как

http://siteaddress.com/cities/city/london

Контроллер: Города

Метод: Город

Параметры: Лондон

http://siteaddress.com/cities/city/birmingham

Контроллер: Города

Метод: Город

Параметры: Бирмингем

Вот снимок структуры проекта

MVC

Я использовал этот плейлист для лучшего понимания. YOUTUBE: создание приложения PHP MVC: введение (часть 1/9)

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...