R
R
rou9h2021-06-18 17:11:21
PHP
rou9h, 2021-06-18 17:11:21

How to make a simple php router (router)?

Please tell me a ready-made example of a simple router. And if possible, describe the principle of operation

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-06-18
@galaxy

Well, right on the matches:

/* index.php */
$controllerClass = ucfirst($_GET['controller']);
$method = $_GET['method'];

require_once("controllers/$controllerClass.php");
$controller = new $controllerClass();
$controller->$method();

(no error checking or protection, of course).
Handles a url like /index.php?controller=user&action=edit
Creates a User class object (from controllers/User.php) and calls the edit() method

S
Sergey delphinpro, 2021-06-18
@delphinpro

A simple php router https://github.com/nikic/FastRoute
And here is a very simple one, from one file https://github.com/dannyvankooten/AltoRouter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question