Я запускаю свой локальный сервер командой:
Код: Выделить всё
php -S localhost:8888 -t publicКод: Выделить всё
project_root
|
|---controllers
| |
| |----notes
| | |
| | |---create.php
| | |---index.php
| | |---show.php
| |
| |
| |---about.php
| |---contact.php
| |---index.php
|
|
|---Core
| |
| |---Database.php
| |---functions.php
| |---Response.php
| |---router.php
| |---Validator
|
|
|---public - document root
| |
| |----index.php
|
|
|---views
| |
| |----notes
| | |---create.php
| | |---index.php
| | |---show.php
| |
| |
| |----partials
| | |---banner.php
| | |---footer.php
| | |---header.php
| | |---nav.php
| |
| |
| |---403.php
| |---404.php
| |---about.php
| |---contact.php
| |---index.php
|
|
|---.htaccess
|---config.php
|---routes.php
Код: Выделить всё
const BASE_PATH = __DIR__.'/../';
// var_dump(BASE_PATH); // "C:\xampp\htdocs\project_root\public/../"
require BASE_PATH.'Core/functions.php';
// php calls this function automatically when a class is required
spl_autoload_register(function ($class) {
require base_path("Core/" . $class . '.php');
});
// appends BASE_PATH to the given path
require base_path('Core/router.php');
Код: Выделить всё
function base_path($path)
{
// append BASE_PATH so we don't have to remember what the path is
return BASE_PATH . $path; // const BASE_PATH = __DIR__.'/../'
}
Код: Выделить всё
function routeToController($uri, $routes) {
if (array_key_exists($uri, $routes)) {
require base_path($routes[$uri]);
} else {
abort();
}
}
function abort($code = 404) {
http_response_code($code);
require base_path("views/{$code}.php");
die();
}
$routes = require base_path('routes.php');
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
routeToController($uri, $routes);
Код: Выделить всё
return [
'/' => 'controllers/index.php',
'/about' => 'controllers/about.php',
'/notes' => 'controllers/notes/index.php',
'/note' => 'controllers/notes/show.php',
'/notes/create' => 'controllers/notes/create.php',
'/contact' => 'controllers/contact.php',
];
Когда я начинаю локальный сервер разработки с помощью команды php -S localhost:8888 -t public все работает как положено, и я могу нажимать ссылки на главную страницу о заметках и контактах для отображения соответствующего документа.
На общем сервере я пробовал различные правила .htaccess, и лучшее, что у меня получилось:
Код: Выделить всё
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^$ /public/$1
Код: Выделить всё
// project_root/views/index.php
// import partials:
Hello. Welcome to the home page.
Код: Выделить всё
aboutКогда я смотрю на сетевой монитор браузера и нажимаю кнопку «О программе» link я получаю следующий метод, имя файла и статус:
Код: Выделить всё
GET
filename /about
Status 302 Found
Код: Выделить всё
GET
filename /
Status 200 OK
Можно ли добиться того, чего я хочу, используя правила .htaccess?< /п>
Подробнее здесь: https://stackoverflow.com/questions/792 ... the-docume
Мобильная версия