dongyin2643 2015-03-03 16:12
浏览 57
已采纳

Laravel定制Facades类无法正常工作

I have the following dir in my laravel-4 application:

app/Latheesan

This has been added to my composer.json like this:

"autoload": {
    ...
    "psr-0": {
        "Latheesan": "app/"
    }
    ...
},

Given that set-up, I want to create a custom Facade class called Helpers so that I can use it for various operations in my app, for e.g. Helpers::getFreeStock();

To achieve this, I have created the following folders & files:

enter image description here

with the following codes in the respective files:

app/Latheesan/System/Helpers/Facades/Helpers.php

<?php namespace Latheesan\System\Helpers;

use Illuminate\Support\Facades\Facade;

class Helpers extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'helpers'; }

}

app/Latheesan/System/Helpers/Helpers.php

<?php namespace Latheesan\System\Helpers;

class Helpers {

    /**
     * Get free stock levels by organisation id.
     * If param $skus is empty, free stock will be calculated for every inventory organisation owns.
     *
     * @param $organisation_id
     * @param array $skus
     * @return array
     */
    public function getFreeStock($organisation_id, $skus = [])
    {
        return 'hello world - todo actual implementation';
    }

}

app/Latheesan/System/Helpers/HelpersServiceProvider.php

<?php namespace Latheesan\System\Helpers;

use Illuminate\Support\ServiceProvider;

class HelpersServiceProvider extends ServiceProvider {

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        $this->app['helpers'] = $this->app->share(function($app)
        {
            return new Helpers;
        });
    }

}

Now I have updated the app/config/app.php like so:

// Add service provider
'Latheesan\System\Helpers\HelpersServiceProvider'

...

// Create alias
'Helpers' => 'Latheesan\System\Helpers\Facades\Helpers'

Finally, to test this, I created the following route:

Route::get('/test', function() {
    dd(Helpers::getFreeStock(1, ['vcf001']));
});

When I visit this /test to test the helpers Facade class, I get the following error:

Class 'Latheesan\System\Helpers\Facades\Helpers' not found

What have I done wrong here? Any idea why this isn't working?

  • 写回答

1条回答 默认 最新

  • dqpu4988 2015-03-04 10:15
    关注

    Thanks to Luceos, I have fixed the problem. I had a typo in the namespace in app/Latheesan/System/Helpers/Facades/Helpers.php file.

    The correct code should be like this:

    <?php namespace Latheesan\System\Helpers\Facades;
    
    use Illuminate\Support\Facades\Facade;
    
    class Helpers extends Facade {
    
        /**
         * Get the registered name of the component.
         *
         * @return string
         */
        protected static function getFacadeAccessor() { return 'helpers'; }
    
    }
    

    After changing that, I had to run composer dump-auto and everything worked great.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算