简体   繁体   English

如何使用撰写自动加载和PSR-4自动加载文件

[英]How to autoload files using compose autoload and psr-4

I'm trying to autoload files and to have an entry point for my app. 我正在尝试自动加载文件并为我的应用提供入口点。 I'm not using framework, using OOP. 我没有使用框架,而是使用OOP。 My composer.json file is: 我的composer.json文件是:

 { "require": { "tebazil/db-seeder": "^0.0.0" }, "autoload": { "psr-4": { "vendor\\\\": "vendor/", "App\\\\": "src/classes/" }, "files": [ "src/functions.php" ] } } 

In this file functions.php I want to include the vendor/autoload.php file. 在这个文件functions.php中,我想包含vendor / autoload.php文件。 I'm not sure how to create this entry point for the app and there to require this vendor/autoload file because I've used frameworks for this goal. 我不确定如何为应用程序创建此入口点,并且在那里需要此供应商/自动加载文件,因为我已经为此目的使用了框架。

This is the current content of the functions.php but there is another issue as I render the index.html view file, but there is ajax request. 这是functions.php的当前内容,但是在渲染index.html视图文件时还有另一个问题,但是存在ajax请求。 However, what is the correct way to have a bootstrap file for the app and autoload necessary files? 但是,为应用程序创建引导文件并自动加载必要文件的正确方法是什么?

 <?php namespace App; require_once __DIR__ . '/../vendor/autoload.php'; use App\\Db; use App\\User; class Functions { public function render() { ob_start(); include(__DIR__ . '/../index.html'); $content = ob_get_contents(); ob_end_clean(); echo $content; } } if(!($_GET && array_key_exists('name', $_GET))) { $functions = new Functions(); $functions->render(); } if($_GET && array_key_exists('name', $_GET)){ $user = new User(); $users = $user->getUsers(); } 

As far as i can see you don't need to register "functions.php" in composer. 据我所知,您不需要在作曲家中注册“ functions.php”。 Your application may have the following structure: 您的应用程序可能具有以下结构:

- app_folder/
  - src/
    - classes/
    - index.html
  - public/
    - functions.php
  - vendor/
    - ...

Where "functions.php" file serves as application entry point. 其中“ functions.php”文件充当应用程序入口点。 Direct your web server to "public" folder, thus all other files will be accessible to your php code, but not to users. 将您的Web服务器定向到“公共”文件夹,这样,您的php代码将可以访问所有其他文件,但用户不能访问。

You may implement your routing or similar tasks in "functions.php" and place components like models and views to "src", and you will get simple yet structured application. 您可以在“ functions.php”中实现路由或类似任务,并将诸如模型和视图之类的组件放置到“ src”,您将获得简单而结构化的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM