douluo2930 2015-12-28 21:37
浏览 357
已采纳

使用psr-4自动加载文件时出现问题

I've been trying to get psr-4 autoloading work for over a week now with no success.

My file structure is as follows:

-Project
  -src
    -classes
       session.php
  -vendor
  index.php

I've created the psr-4 autoload function as follows:

 "autoload": {
        "psr-4": {
         "classes\\": "src/classes"
    }

}

after using composer dump-autoload -0 ,inside my session.php class I gave the namespace:

namespace classes;

class session{

public static function exist($name){

    return(isset($_SESSION[$name])) ? true : false;
}

I then required the autoloader and used the use function to name the session class as follows:

use src\classes\session as session;

require_once('vendor/autoload.php');

session::put('test', 'test');

after opening up the index.php page, I get a

Fatal error: Class 'src\classes\session' not found in /var/www/test/Project/index.php on line 10

is my directory structure / php correct? I've tried many different guides online and can't seem to get it to work.

  • 写回答

2条回答 默认 最新

  • doujingxi3356 2015-12-28 22:20
    关注

    Most simple solution:

    use classes\session;
    
    require_once('vendor/autoload.php');
    
    session::put('test', 'test');
    

    Unrelated

    However, you probably don't want to use classes as a vendor namespace, but instead adjust a few things here and there:

    Directory structure

    -Project
      -src
        Session.php
      -public
        index.php
      -vendor
    

    Autoloading configuration in composer.json

    {
        "autoload": {
            "psr-4": {
                "Juakali\\": "src"
            }
        }
    }
    

    Replace Juakali with a vendor namespace you prefer, this is just a suggestion. Ideally, if you intend to publish your package, it should be one that isn't already claimed by someone else, see https://packagist.org.

    For reference, see

    Juakali\Session

    Use the aforementioned vendor namespace of your choice:

    namespace Juakali;
    
    class Session
    {
        public static function exist($name)
        {
            return isset($_SESSION[$name]);
        }
    }
    

    Consider using a widely used coding style, for example PSR-2.

    For reference, see

    index.php

    Assuming that you want to expose index.php as the entry point for a web application, move it into a directory which you feel confident to expose as a document root of your web server, adjust the import in index.php, as well as the path to vendor/autoload.php:

    use Juakali\Session;
    
    require_once __DIR__ . '/../vendor/autoload.php';
    
    Session::put('test', 'test');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#vscode#的问题:ESP32开发板对接MQTT实现小灯泡的开关
  • ¥15 TMC2209串口模式下读取不到寄存器的值串口助手蓝色字体是发过去的消息,绿色字体是收到的消息,第二行发送读取寄存器的指令但是没有读取到寄存器的值串口助手如下图:接线如下图,如何解决?
  • ¥15 高通安卓11提取完整线刷包软件,或者优博讯dt50顺丰刷机包
  • ¥20 C,有个译码器,换了信道就跑不出原来数据
  • ¥15 MIMIC数据库安装问题
  • ¥60 基于JTag协议开发Fpga下载器上位机,哪位大🐂有偿指导?
  • ¥20 全书网Java爬取数据
  • ¥15 怎么获取红包封面的原始链接,并且获取红包封面序列号
  • ¥100 微信小程序跑脚本授权的问题
  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏