dingo-route 不能根据路由别名获取路由?
问题描述:
使用dingo官方文档的方法获取路又失败。
// 路由文件
$api = app('Dingo\Api\Routing\Router');
$api->version('v1', [
'namespace' => 'App\Http\Controllers\Api',
'middleware' => [
'serializer:array',
'bindings',
'cors',
],
], function ($api) {
// ********************* 小程序前台用户 token 非验证路由组 ********************* //
$api->group(['middleware' => [], 'prefix' => 'miniprogram'], function ($api) {
$api->any('pay/{mini_appid}/notify', 'PaymentController@wechatNotify')->name('api.miniprogram.pay.notify');
});
});
// 控制器
use Dingo\Api\Routing\UrlGenerator;
use Illuminate\Http\Request;
public function index(Request $request)
{
$UrlGenerator = new UrlGenerator($request);
$cla_url = $UrlGenerator->version('v1')->route('api.miniprogram.pay.notify', ['mini_appid' => 'asd123']);
$app_url = app(UrlGenerator::class)->version('v1')->route('api.miniprogram.pay.notify', ['mini_appid' => 'asd123']);
return $this->response->array([$cla_url, $app_url]);
}
报错信息:
"message": "Call to a member function getByName() on null",
"status_code": 500,
"debug": {
"line": 301,
"file": "/vagrant/code/suokewangluo/vendor/laravel/framework/src/Illuminate/Routing/UrlGenerator.php",
"class": "Symfony\\Component\\Debug\\Exception\\FatalThrowableError",
或者谁有这方面的经验分享下。
解决方案是使用完整命名空间生成路由,或者使用全局函数url创建路由。