公共文件夹内的静态文件在 PHP MVC 架构中不起作用

问题描述 投票:0回答:1

我的项目结构

  • 核心

    路由器.php

  • http

    • 控制器

      index.php

  • 公众

    CSS

    js

    img

    index.php

  • 浏览量

routes.php

index.php代码

<?php

use Core\Router;

const BASE_PATH = __DIR__.'/../';

session_start();

require BASE_PATH.'vendor/autoload.php';
require BASE_PATH.'Core/functions.php';
require BASE_PATH.'bootstrap.php';

$router = new Router();

require BASE_PATH.'routes.php';


$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$method = $_POST['_method'] ?? $_SERVER['REQUEST_METHOD'];

try {
    $router->route($uri, $method);
} catch (Exception $e) {
    echo $e->getMessage();
}

Router.php代码

<?php

namespace Core;

use Core\Middleware\Middleware;
use Exception;
use JetBrains\PhpStorm\NoReturn;

class Router
{
    protected array $routes = [];

    public function get($uri, $controller): void
    {
        $this->add('GET', $uri, $controller);
    }

    public function add($method, $uri, $controller): static
    {
        $this->routes[] = [
            'uri' => $uri,
            'controller' => $controller,
            'method' => $method,
            'middleware' => null
        ];

        return $this;
    }

    public function post($uri, $controller): void
    {
        $this->add('POST', $uri, $controller);
    }

    public function delete($uri, $controller): static
    {
        return $this->add('DELETE', $uri, $controller);
    }

    public function patch($uri, $controller): static
    {
        return $this->add('PATCH', $uri, $controller);
    }

    public function put($uri, $controller): static
    {
        return $this->add('PUT', $uri, $controller);
    }

    public function only($key): static
    {
        $this->routes[array_key_last($this->routes)]['middleware'] = $key;

        return $this;
    }

    /**
     * @throws Exception
     */
    public function route($uri, $method)
    {
        foreach ($this->routes as $route) {
            if ($route['uri'] === $uri && $route['method'] === strtoupper($method)) {
                Middleware::resolve($route['middleware']);

                return require base_path('Http/controllers/'.$route['controller']);
            }
        }

        $this->abort();
    }

    #[NoReturn] protected function abort($code = 404): void
    {
        http_response_code($code);

        require base_path("views/{$code}.php");

        die();
    }

    public function previousUrl()
    {
        return $_SERVER['HTTP_REFERER'];
    }

}

routes.php代码

<?php
/** @var Router $router */

use Core\Router;

$router->get('/', 'index.php');
$router->get('/contact', 'contact.php');
$router->get('/about', 'about.php');
$router->get('/service', 'service.php');
$router->get('/menu', 'menu.php');
$router->get('/team', 'team.php');
$router->get('/testimonial', 'testimonial.php');
$router->get('/booking', 'booking/booking.php');

$router->get('/register', 'register/create.php');
$router->get('/login', 'session/create.php');

$router->get('/cart', 'cart/index.php');

公共文件夹中存在的所有静态文件和库,当我调用它们时,它们在项目中的其他页面上不起作用。

我使用 php -S lcoalhost:8080 在公共文件夹中运行项目

我尝试更改静态文件的路径,但它不起作用,我也尝试将 .htaccess 添加到项目中,但也不起作用

javascript php css file model-view-controller
1个回答
0
投票

那是因为你的 php 内置的“服务器”并不是真正的服务器(因为缺乏更好的描述)。您应该学习如何使用 docker 设置 nginx/fpm 服务器。

但是,简而言之,您可以创建一个单独的文件,在针对

index.php
运行之前尝试加载现有文件。

您将服务器初始化命令更改为:

php -S 127.0.0.1:8080 ./public/built-in.php

有点像这个练习(有点过时,但应该解释一下这个想法)。

© www.soinside.com 2019 - 2024. All rights reserved.