laravel 调用未定义的方法 Illuminate\Routing\Route::getUri()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41863384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:12:08  来源:igfitidea点击:

Call to undefined method Illuminate\Routing\Route::getUri()

phplaravelrestdingo-api

提问by arif hasnat

I am trying to make rest api with dingo for laravel 5.3 . I have setup dingo on my project and created a api route like this for test .

我正在尝试使用 dingo 为 laravel 5.3 制作 rest api。我在我的项目上设置了 dingo 并为 test 创建了一个像这样的 api 路由。

 $api->version('v1', function ($api) {
    $api->get('hello',function (){

        return "hello";
    });

});

But when i run http://localhost:8000/api/hello

但是当我运行 http://localhost:8000/api/hello

Then

然后

{
message: "Call to undefined method Illuminate\Routing\Route::getUri()",
code: 1,
status_code: 500,
debug: {
line: 98,
file: "C:\xampp\htdocs\apiTest\vendor\dingo\api\src\Routing\Adapter\Laravel.php",
class: "Symfony\Component\Debug\Exception\FatalErrorException",
trace: [
"#0 {main}"
]
}
}

is shown .

显示。

i have searched and find this solution Call to undefined method Illuminate\Routing\Route::get()

我已经搜索并找到了这个解决方案 Call to undefined method Illuminate\Routing\Route::get()

but when i used

但是当我用

use Illuminate\Support\Facades\Route;

then this problem shown

然后这个问题显示

FatalErrorException in Laravel.php line 116:
Call to undefined method Illuminate\Support\Facades\Route::where()

This is the Laravel.php file

这是 Laravel.php 文件

<?php

namespace Dingo\Api\Routing\Adapter;

use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Routing\RouteCollection;
use Dingo\Api\Contract\Routing\Adapter;
use Dingo\Api\Exception\UnknownVersionException;

class Laravel implements Adapter
{
    /**
     * Laravel router instance.
     *
     * @var \Illuminate\Routing\Router
     */
    protected $router;

    /**
     * Array of registered routes.
     *
     * @var array
     */
    protected $routes = [];

    /**
     * Old routes already defined on the router.
     *
     * @var \Illuminate\Routing\RouteCollection
     */
    protected $oldRoutes;

    /**
     * Create a new laravel routing adapter instance.
     *
     * @param \Illuminate\Routing\Router $router
     *
     * @return void
     */
    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    /**
     * Dispatch a request.
     *
     * @param \Illuminate\Http\Request $request
     * @param string                   $version
     *
     * @return mixed
     */
    public function dispatch(Request $request, $version)
    {
        if (! isset($this->routes[$version])) {
            throw new UnknownVersionException;
        }

        $routes = $this->mergeExistingRoutes($this->routes[$version]);

        $this->router->setRoutes($routes);

        return $this->router->dispatch($request);
    }

    /**
     * Merge the existing routes with the new routes.
     *
     * @param \Illuminate\Routing\RouteCollection $routes
     *
     * @return \Illuminate\Routing\RouteCollection
     */
    protected function mergeExistingRoutes(RouteCollection $routes)
    {
        if (! isset($this->oldRoutes)) {
            $this->oldRoutes = $this->router->getRoutes();
        }

        foreach ($this->oldRoutes as $route) {
            $routes->add($route);
        }

        return $routes;
    }

    /**
     * Get the URI, methods, and action from the route.
     *
     * @param mixed                    $route
     * @param \Illuminate\Http\Request $request
     *
     * @return array
     */
    public function getRouteProperties($route, Request $request)
    {
        return [$route->getUri(), $route->getMethods(), $route->getAction()];
    }

    /**
     * Add a route to the appropriate route collection.
     *
     * @param array  $methods
     * @param array  $versions
     * @param string $uri
     * @param mixed  $action
     *
     * @return \Illuminate\Routing\Route
     */
    public function addRoute(array $methods, array $versions, $uri, $action)
    {
        $this->createRouteCollections($versions);

        $route = new Route($methods, $uri, $action);
        $route->where($action['where']);

        foreach ($versions as $version) {
            $this->routes[$version]->add($route);
        }

        return $route;
    }

    /**
     * Create the route collections for the versions.
     *
     * @param array $versions
     *
     * @return void
     */
    protected function createRouteCollections(array $versions)
    {
        foreach ($versions as $version) {
            if (! isset($this->routes[$version])) {
                $this->routes[$version] = new RouteCollection;
            }
        }
    }

    /**
     * Get all routes or only for a specific version.
     *
     * @param string $version
     *
     * @return mixed
     */
    public function getRoutes($version = null)
    {
        if (! is_null($version)) {
            return $this->routes[$version];
        }

        return $this->routes;
    }

    public function getIterableRoutes($version = null)
    {
        return $this->getRoutes($version);
    }

    /**
     * Set the routes on the adapter.
     *
     * @param array $routes
     *
     * @return void
     */
    public function setRoutes(array $routes)
    {
        $this->routes = $routes;
    }

    /**
     * Prepare a route for serialization.
     *
     * @param mixed $route
     *
     * @return mixed
     */
    public function prepareRouteForSerialization($route)
    {
        $route->prepareForSerialization();

        return $route;
    }

    /**
     * Gather the route middlewares.
     *
     * @param \Illuminate\Routing\Route $route
     *
     * @return array
     */
    public function gatherRouteMiddlewares($route)
    {
        return $this->router->gatherRouteMiddlewares($route);
    }
}

Has there any solution ? Thanks

有什么解决办法吗?谢谢

回答by George John

Use $route->uri()

$route->uri()

insted of $route->getUri()

插入的 $route->getUri()

create a pull request to dingo api after updating this

更新后创建一个到 dingo api 的拉取请求