简体   繁体   中英

how to autoload with composer.json?

I am using these require statements in my root folder of project directory (in 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");

and now I am trying to use composer autoload to load on demand by using composer.json file to remove all above require with single one :

require("./vendor/autoload.php"); 

My composer.json file is as follows

{
    "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/"]
    }
}

It works fine if I remove "lib/" and "routes/"

please provide me proper solution for this problem I'll be thankful to all.

oho.. I got what I was doing wrong

actually the util.php and routes.php does not contains a class to initialize the autoload_classmap.php

my util.php was before like this :

<?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);
    }

Now I just make it wrapped with a class with static methods and these methods can be access through HTMLutill::

<?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);
    }

}

Now my app is working fine. happy coding guys..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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