如何使用 composer.json 自动加载?
how to autoload with composer.json?
我在项目目录的根文件夹中使用这些 require 语句(在 index.php 中)。
require("./models/college/collegeModel.php");
require("./routes/routes.php");
require("./controllers/college/collegeController.php");
require("./controllers/home/homeController.php");
require("./controllers/login/loginController.php");
require("./controllers/logout/logoutController.php");
require("./controllers/public/publicController.php");
require("./lib/util.php");
现在我正在尝试使用 composer autoload 来按需加载,方法是使用 composer.json 文件删除上述所有要求,只需要一个:
require("./vendor/autoload.php");
我的composer.json文件如下
{
"name": "vermajnv/web",
"authors": [
{
"name": "vermajnv",
"email": "nayanrahul.jnv@gmail.com"
}
],
"require": {},
"autoload": {
"classmap": ["models/college", "controllers/college",
"controllers/home", "controllers/login", "controllers/logout",
"controllers/public", "lib/", "routes/"]
}
}
如果我删除 "lib/" 和 "routes/"
就可以正常工作
请为我提供解决此问题的正确方法,我将不胜感激。
oho..我做错了什么
实际上util.php和routes.php不包含一个class来初始化autoload_classmap.php
我的util.php以前是这样的:
<?php
public function baseUrl($url) {
$contaxtPath = "/" . explode("/", $_SERVER['REQUEST_URI'])[1];
return $contaxtPath . $url;
}
public function redirect($to) {
$url = baseUrl($to);
header("Location:" . $url, 302);
}
现在我只是用 class 和 static 方法包裹它,这些方法可以通过 HTMLutill::[=27= 访问]
<?php
class HTMLutil {
// static method inside a class are visible every where without creating instance of class (HTMLutil) we can access it by HTML::baseUrl();
public static function baseUrl($url) {
$contaxtPath = "/" . explode("/", $_SERVER['REQUEST_URI'])[1];
return $contaxtPath . $url;
}
public static function redirect($to) {
$url = self::baseUrl($to);
header("Location:" . $url, 302);
}
}
现在我的应用程序运行良好。
快乐的编码家伙..
我在项目目录的根文件夹中使用这些 require 语句(在 index.php 中)。
require("./models/college/collegeModel.php");
require("./routes/routes.php");
require("./controllers/college/collegeController.php");
require("./controllers/home/homeController.php");
require("./controllers/login/loginController.php");
require("./controllers/logout/logoutController.php");
require("./controllers/public/publicController.php");
require("./lib/util.php");
现在我正在尝试使用 composer autoload 来按需加载,方法是使用 composer.json 文件删除上述所有要求,只需要一个:
require("./vendor/autoload.php");
我的composer.json文件如下
{
"name": "vermajnv/web",
"authors": [
{
"name": "vermajnv",
"email": "nayanrahul.jnv@gmail.com"
}
],
"require": {},
"autoload": {
"classmap": ["models/college", "controllers/college",
"controllers/home", "controllers/login", "controllers/logout",
"controllers/public", "lib/", "routes/"]
}
}
如果我删除 "lib/" 和 "routes/"
就可以正常工作请为我提供解决此问题的正确方法,我将不胜感激。
oho..我做错了什么
实际上util.php和routes.php不包含一个class来初始化autoload_classmap.php
我的util.php以前是这样的:
<?php
public function baseUrl($url) {
$contaxtPath = "/" . explode("/", $_SERVER['REQUEST_URI'])[1];
return $contaxtPath . $url;
}
public function redirect($to) {
$url = baseUrl($to);
header("Location:" . $url, 302);
}
现在我只是用 class 和 static 方法包裹它,这些方法可以通过 HTMLutill::[=27= 访问]
<?php
class HTMLutil {
// static method inside a class are visible every where without creating instance of class (HTMLutil) we can access it by HTML::baseUrl();
public static function baseUrl($url) {
$contaxtPath = "/" . explode("/", $_SERVER['REQUEST_URI'])[1];
return $contaxtPath . $url;
}
public static function redirect($to) {
$url = self::baseUrl($to);
header("Location:" . $url, 302);
}
}
现在我的应用程序运行良好。 快乐的编码家伙..