You need to implement MVC without frameworks. In general, in the .htaccess
file everything is redirected to index.php
RewriteEngine On RewriteRule .* index.php [L]
There load.php
ini_set('display_errors', 1); require_once 'load.php';
And in it components MVC are loaded and routing is started
require_once 'core/route.php'; require_once 'core/model.php'; require_once 'core/view.php'; require_once 'core/controller.php'; //Запуск роутинга Route::start();
Routing takes the url and breaks it, here is the route.php
class Route { static function start() { // контроллер и действие по умолчанию $controller_name = 'Main'; $action_name = 'index'; $routes = explode('/', $_SERVER['REQUEST_URI']); // получаем имя контроллера if ( !empty($routes[1]) ) { $controller_name = $routes[0]; } // получаем имя экшена if ( !empty($routes[2]) ) { $action_name = $routes[2]; } // добавляем префиксы $model_name = 'Model_'.$controller_name; $controller_name = 'Controller_'.$controller_name; $action_name = 'action_'.$action_name; // подцепляем файл с классом модели (файла модели может и не быть) $model_file = strtolower($model_name).'.php'; $model_path = "models/".$model_file; if(file_exists($model_path)) { include "models/".$model_file; } // подцепляем файл с классом контроллера $controller_file = strtolower($controller_name).'.php'; $controller_path = "controllers/".$controller_file; if(file_exists($controller_path)) { include "controllers/".$controller_file; } else { /* правильно было бы кинуть здесь исключение, но для упрощения сразу сделаем редирект на страницу 404 */ //Route::ErrorPage404(); echo 123; } // создаем контроллер $controller = new $controller_name; $action = $action_name; if(method_exists($controller, $action)) { // вызываем действие контроллера $controller->$action(); } else { // здесь также разумнее было бы кинуть исключение //Route::ErrorPage404(); echo 123; } } function ErrorPage404() { $host = 'http://'.$_SERVER['HTTP_HOST'].'/'; header('HTTP/1.1 404 Not Found'); header("Status: 404 Not Found"); header('Location:'.$host.'404'); } }
In general, the project is located in the createownmvc
folder and when I createownmvc
http: // localhost / createownmvc / prints 123, as it should, because I don’t have such a controller. But if I try to go somewhere like localhost / createownmvc / somethink then it does not output 123 and I get an error from apache itself, that is, the routing does not catch this url and does not try to find the createownmvc controller with somethink method, it’s just Apache
Not Found The requested URL /createownmvc/somethink was not found on this server. Apache/2.4.18 (Ubuntu) Server at localhost Port 80
So the question is, How can I make the routing catch such url? And now, when you enter http: // localhost / createownmvc / routing, it is looking for a controller to createownmvc, how to change it so that you can search for controllers like http: // localhost / createownmvc / controller / action ?
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
you try to add beforeRewriteRule .* index.php [L]
? - Visman