Previous route name in Laravel 5.1-5.8

前端 未结 4 1157
迷失自我
迷失自我 2020-12-10 13:22

I try to find name of previous route in Laravel 5.1. With:

{!! URL::previous() !!}

I get the route url, but I try to get route name like I

4条回答
  •  悲&欢浪女
    2020-12-10 13:34

    I've created a helper function like this.

    /**
     * Return whether previous route name is equal to a given route name.
     *
     * @param string $routeName
     * @return boolean
     */
    function is_previous_route(string $routeName) : bool
    {
        $previousRequest = app('request')->create(URL::previous());
    
        try {
            $previousRouteName = app('router')->getRoutes()->match($previousRequest)->getName();
        } catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $exception) {
            // Exception is thrown if no mathing route found.
            // This will happen for example when comming from outside of this app.
            return false;
        }
    
        return $previousRouteName === $routeName;
    }
    

提交回复
热议问题