我在我的" config / app.php'中添加了自定义外观。在我的laravel项目下的'别名'
'GeoLocation' => '\App\Facades\GeoLocation',
自定义类的文件夹位于'app/Facades/GeoLocation.php'
,'app/Providers/GeoLocationServiceProvider.php'
中的服务提供商
如何在app.php中声明正确的别名,以便能够正确加载Facade?错误消息是:
ErrorException in AliasLoader.php line 63:
Class 'Facades\GeoLocation' not found
这是我的门面:
<?php namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class GeoLocation extends Facade {
protected static function getFacadeAccessor() { return 'geolocation'; }
}
可能是我的服务提供商的返回值不正确吗?
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class GeoLocationServiceProvider extends ServiceProvider {
public function register() {
\App::bind('geolocation', function()
{
return new GeoLocation;
});
}
}
对于测试,我创建了另一个名为自定义函数的服务提供者:
<?php namespace App\Helpers;
class CustomFunction {
//Generate random float between -1 and 1
function f_rand($min = 0, $max = 1, $mul = 1000000) {
if ($min>$max) return false;
return mt_rand($min*$mul, $max*$mul) / $mul;
}
function test() {
echo "OK";
}
}
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class CustomFunctionServiceProvider extends ServiceProvider {
public function register() {
\App::bind('customfunctions', function()
{
return new CustomFunction;
});
}
}
答案 0 :(得分:1)
最简单的方法是将命名空间更改为文件app / Facades / GeoLocation.php&#39;到App\Facades
;
然后将别名注册更新为
'GeoLocation' => 'App\Facades\GeoLocation',