douan1893 2016-11-14 14:48
浏览 165
已采纳

在没有composer dump-autoload的Laravel中加载自定义php文件

I'm new in Laravel. I've been followed few threads of how to load custom php library to Laravel 4,2 without dump autoload. So far I'm unable or I calling the function incorrect.

What I have so far is:

  1. I file autoload_classmap.php I've added my class

    'ImageResize' => $baseDir . '/app/libraries/ImageResize.php',

  2. In file autoload_static.php I've added

    'ImageResize' => __DIR__ . '/../..' . '/app/libraries/ImageResize.php',

Then in my controller in the function where I want to be shown this class I've tried like this

public function upload() {

$FmyFunction1 = new \ImageResize(); 
    return View::make('site.admin.upload', [
        'FmyFunction1' => $FmyFunction1
    ]);
}

Result is when I try to load /upload page I've got error:

'Class 'ImageResize' not found'

Is this error from wrong calling the class and/or error from not include correctly the class in Laravel at all? Can anyone help me?

ps. The reason I can't use dump autoload is because I have only ftp access to the host and I don't have SSH...

  • 写回答

2条回答 默认 最新

  • doufuhao8085 2016-11-14 15:13
    关注

    You can add your namespace to the autoloader like this :

    $loader = require 'vendor/autoload.php';
    $loader->add('NameSpace', 'Path to directory'); // PSR-0 loading
    $loader->addPsr4('NameSpace\\', 'Path to directory'); // PSR-4 loading
    

    Doc : https://getcomposer.org/apidoc/1.0.0/Composer/Autoload/ClassLoader.html#method_add


    The code needs to be added in the bootstrap.php file : you need to extend the base autoloader which is loaded with this line : require __DIR__.'/../vendor/autoload.php';

    replace this line with $loader = require __DIR__.'/../vendor/autoload.php'; and add your custom namespace to the autoloader.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名