PHP 上的位置或路径问题

这是我用于我的项目的目录结构:


assets

connection

    connection.php

controller

    Controller.php

include

    header.php

    footer.php

model

    Model.php

process

    add-data.php

view

    index.pp

    sales-form.php

    report.php

config.php

index.php

首先,它加载index.php包含此代码的文件:


require_once("controller/Controller.php");

$controller = new Controller;

$controller->index();

您会看到它index()从 Controller.php 文件中加载方法,代码如下:


public function index () {      

    require_once('view/index.php');

}

现在index.php文件容器此代码:


<?php require_once("include/header.php"); ?>

    <a class="btn btn-primary" href="view/sales-form.php">Record a new sales</a>

    <a class="btn btn-success" href="view/report.php">Search Report</a> 

<?php require_once("include/footer.php"); ?>

现在,您可以看到Record a new sales button 链接是这样的:view/sales-form.php但是当我转到该链接时,它向我显示了错误消息:


警告:require_once(config.php):无法打开流:第 1 行 D:\xampp\htdocs\job-tasks\include\header.php 中没有这样的文件或目录


该 sales-form.php 文件包含此代码


<?php require_once("../include/header.php"); ?>

<?php require_once("../include/footer.php"); ?>

这个 header.php 代码如下:


<?php require_once('config.php'); ?>

html code....

你能告诉我如何解决与链接/位置相关的问题吗?


手掌心
浏览 146回答 2
2回答

侃侃无极

你header.php应该是:<?php require_once(__DIR__. '/../config.php'); ?>因为config.php在include文件夹之外,因此您需要后退一步(转到项目的根目录)然后访问它。您应该将所有端点转换为使用基于您的项目将导致控制器的根路径,因此:控制器.php:<?php&nbsp;// get the model classrequire(__DIR__ .'/../model/Model.php');// create Controller classclass Controller {&nbsp; &nbsp; // hold the model object&nbsp; &nbsp; public $model;&nbsp; &nbsp; public function __construct () {&nbsp; &nbsp; &nbsp; &nbsp; // create model object&nbsp; &nbsp; &nbsp; &nbsp; $this->model = new Model();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; public function index () {&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; require_once(__DIR__ .'/../view/index.php');&nbsp; &nbsp; }&nbsp; &nbsp; public function salesForm () {&nbsp; &nbsp; &nbsp; &nbsp; require_once(__DIR__ .'/../view/sales-form.php');&nbsp; &nbsp; }}头文件<?php require_once(__DIR__ .'/../config.php'); ?>模型.phprequire_once(__DIR__ .'/../config.php');

狐的传说

在从 controller.php 文件调用控制器之前,您可能必须包含如下配置文件:<?php&nbsp;require_once('../config.php');&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP