No hint path defined for [sudosu] 问题如果解决, 和源码解读
1. 运行环境
“laravel/sail”: “^1.0.1”
1). 当前使用的 Laravel 版本?
Laravel Framework 9.13.0
2). 当前使用的 php/php-fpm 版本?
PHP 版本: PHP 8.1.6
2. 问题描述?
阅读 L02 Laravel 教程 - Web 开发实战进阶 ( Laravel 9.x ) 课程, 使用 “viacreative/sudo-su”: “~1.1” 包, 出现 No hint path defined for [sudosu]
. 错误
App\Providers\AppServiceProvider
已添加服务供应商
对应的资源已经发布
php artisan vendor:publish --provider="VIACreative\SudoSu\ServiceProvider"
阅读源代码 vendor/viacreative/sudo-su/src/ServiceProvider.php
后发现 要想服务注册成功, 就得通过tldIsAllowed() 方法的认证, 该方法逻辑将当前域名以 .
为分隔符,分割成数组获取最后一个, 然后和配置文件sudosu.php
中的 allowed_tlds 数组进行计较, 看看allowed_tlds有没有,如果有就注册服务, 所以我们直接修改sudosu.php
文件直接将我们直接算出域名以.
分割成数组的最后一个值填写上去, 例如
127.0.0.1
'allowed_tlds' => ['dev', 'local','1'],
www.zfq6.com
'allowed_tlds' => ['dev', 'local','com'],
localhost
'allowed_tlds' => ['dev', 'local','localhost'],
public function register()
{
if ($this->configExists() && $this->tldIsAllowed()) {
$this->app->register(RouteServiceProvider::class);
}
}
public function boot()
{
$this->publishes([
__DIR__ . '/../resources/assets/compiled' => public_path('sudo-su/'),
], 'public');
$this->publishes([
__DIR__.'/../config/sudosu.php' => config_path('sudosu.php')
], 'config');
if ($this->configExists() && $this->tldIsAllowed()) {
$this->registerViews();
}
}
protected function registerViews()
{
$this->loadViewsFrom(__DIR__ . '/../resources/views', 'sudosu');
// Add an inline view composer for the user-selector
View::composer('sudosu::user-selector', function ($view) {
$sudosu = App::make(SudoSu::class);
$view->with([
'users' => $sudosu->getUsers(),
'hasSudoed' => $sudosu->hasSudoed(),
'originalUser' => $sudosu->getOriginalUser(),
'currentUser' => Auth::user()
]);
});
}
protected function tldIsAllowed()
{
$requestTld = $this->getRequestTld();
$allowedTlds = Config::get('sudosu.allowed_tlds');
return in_array($requestTld, $allowedTlds);
}
protected function getRequestTld()
{
$requestHost = parse_url(Request::url())['host'];
$exploded = explode('.', $requestHost);
$requestTld = end($exploded);
return $requestTld;
}
protected function configExists()
{
return is_array(Config::get('sudosu.allowed_tlds'));
}