W
W
whiteleaf2014-06-10 12:29:04
PHP
whiteleaf, 2014-06-10 12:29:04

How to properly build PHP routing?

Hello,
As you understand from the title, I have only recently started to study programming and it is very important for me to understand how to write projects from scratch, and gain experience in the process of writing.
But I came to the issue of routing and got stuck. I had no experience writing websites even without CNC.
Tell me, please, about these moments:
1. I tried to write a router in this way

<?php

namespace library;


class Router {


    function __construct(){
        $uri = $_SERVER['REQUEST_URI'];

        $uri = trim($uri,'/');
        $uri = explode('/',$uri);
        print_r($_GET);
        echo $_GET[0];
        echo '<br>';

        if($uri[0] == ""){
            $path = '\controller\IndexController';
            $controller = new $path();
            $method = 'dispatch';
            $controller->$method();
        }else{
            $path = '\controller\IndexController';
            $controller = new $path();
            $method = 'dispatch';
            if(!method_exists($controller,$method)){

            }
            $controller->$method();
        }
    }
    public function getController(){

    }
}

But I see that as a rule, everyone takes the address not from REQUEST_URI, but from $_GET. There is a reason for this, or there is no difference.
2. I just pulled out the controller from the URL and the action is a banal assignment of $uri[0], $uri[1], and in frameworks there are definitions using rules and regular expressions. What does it give?
Perhaps there is something to read somewhere for people like me who need to understand how these elementary things work?
Thank you all in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-06-10
@whiteleaf

Look at the option that is provided in Symfony / Routing (not so much the implementation, but how routes are defined in general) and try to implement it in a similar way.
In essence, it all comes down to forming a regular expression from a rule. For example, a rule of the form
where route is our url template, and requirements - parameter validation (optional)
will generate a regular expression of the form:
where (?P<id>\d+)is a named subpattern (you can do it easier, but it's more convenient). From version 5.2+ there may be options like this:

(?P<id>\d+)
(?<id>\d+)
(?'id'\d+)

Next, when checking for matches with routes, your router simply goes through the array of rules (essentially an array of regular expressions) and sees if it matches any rule.
Actually for learning purposes, this is the coolest way. Because there is where to think about the formation of a new line, and you can deal with regular expressions quite well ... The latter will help you not only in PHP, but also in JS (a little), and just in life (heaps of IDEs and editors have search and replace by regular expressions, which is sometimes very useful).
Similar implementation in Yii, and in most other frameworks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question