return redirect ()->route ('users.show', [$user]); 路由调用时该怎么传入参数?

看源码,路由调用时,参数应该传入数组:

    public function route($route, $parameters = [], $status = 302, $headers = [])
    {
        return $this->to($this->generator->route($route, $parameters), $status, $headers);
    }

官方文档中给了个例子,

Route::get('user/{id}/profile', function ($id) {
    //
})->name('profile');

$url = route('profile', ['id' => 1]);

标题中的语句,参数写成了 [$user],这种写法不太理解。简写的话不应该是 compact($user) 吗?

而在 _header.blade.php 中,则完全没有使用数组,只写了值:
<a href="{{ route('users.show', Auth::user()->id) }}">个人中心</a>

经测试,这几种写法效果一样。这是 PHP 的语言特性吗?有点迷惑

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
__yu
最佳答案
public function route($route, $parameters = [], $status = 302, $headers = [])
{
      return $this->to($this->generator->route($route, $parameters), $status, $headers);
}

注意这个方法 $this->generator->route()继续看下去

    // vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php

    // Get the URL to a named route.
    public function route($name, $parameters = [], $absolute = true)
    {
        if (! is_null($route = $this->routes->getByName($name))) {
            return $this->toRoute($route, $parameters, $absolute);
        }

        throw new InvalidArgumentException("Route [{$name}] not defined.");
    }

    // Get the URL for a given route instance.
    protected function toRoute($route, $parameters, $absolute)
    {
        return $this->routeUrl()->to(
            $route, $this->formatParameters($parameters), $absolute
        );
    }

    // Format the array of URL parameters.
    public function formatParameters($parameters)
    {
        $parameters = Arr::wrap($parameters);

        foreach ($parameters as $key => $parameter) {
            if ($parameter instanceof UrlRoutable) {
                $parameters[$key] = $parameter->getRouteKey();
            }
        }

        return $parameters;
    }
    // vendor\laravel\framework\src\Illuminate\Support\Arr.php
     /**
     * If the given value is not an array, wrap it in one.
     *
     * @param  mixed  $value
     * @return array
     */
    public static function wrap($value)
    {
        return ! is_array($value) ? [$value] : $value;
    }

到这结束, 这个参数不是数组会转换为数组 :joy:

6年前 评论
讨论数量: 5
__yu
public function route($route, $parameters = [], $status = 302, $headers = [])
{
      return $this->to($this->generator->route($route, $parameters), $status, $headers);
}

注意这个方法 $this->generator->route()继续看下去

    // vendor\laravel\framework\src\Illuminate\Routing\UrlGenerator.php

    // Get the URL to a named route.
    public function route($name, $parameters = [], $absolute = true)
    {
        if (! is_null($route = $this->routes->getByName($name))) {
            return $this->toRoute($route, $parameters, $absolute);
        }

        throw new InvalidArgumentException("Route [{$name}] not defined.");
    }

    // Get the URL for a given route instance.
    protected function toRoute($route, $parameters, $absolute)
    {
        return $this->routeUrl()->to(
            $route, $this->formatParameters($parameters), $absolute
        );
    }

    // Format the array of URL parameters.
    public function formatParameters($parameters)
    {
        $parameters = Arr::wrap($parameters);

        foreach ($parameters as $key => $parameter) {
            if ($parameter instanceof UrlRoutable) {
                $parameters[$key] = $parameter->getRouteKey();
            }
        }

        return $parameters;
    }
    // vendor\laravel\framework\src\Illuminate\Support\Arr.php
     /**
     * If the given value is not an array, wrap it in one.
     *
     * @param  mixed  $value
     * @return array
     */
    public static function wrap($value)
    {
        return ! is_array($value) ? [$value] : $value;
    }

到这结束, 这个参数不是数组会转换为数组 :joy:

6年前 评论

@__yu 非常感谢。你的钻研精神令人钦佩。
“不是数组会转换为数组”,这个理解了。
但是 [$user] 这样的没有 key 的数组,含义应该是 [0 => $user],对系统怎么处理还有点疑问,可能在下面这段代码中吧,还没看明白


    public function to($route, $parameters = [], $absolute = false)
    {
        $domain = $this->getRouteDomain($route, $parameters);

        // First we will construct the entire URI including the root and query string. Once it
        // has been constructed, we'll make sure we don't have any missing parameters or we
        // will need to throw the exception to let the developers know one was not given.
        $uri = $this->addQueryString($this->url->format(
            $root = $this->replaceRootParameters($route, $domain, $parameters),
            $this->replaceRouteParameters($route->uri(), $parameters)
        ), $parameters);

        if (preg_match('/\{.*?\}/', $uri)) {
            throw UrlGenerationException::forMissingParameters($route);
        }
        ...
    }
5年前 评论
__yu

@大风

file
你这个 $user 应该是个模型吧?

5年前 评论

@大风 这里我也没看懂

5年前 评论

@等车的猪 继承楼上接着瞎搞.

这里传 $user 的话, 会被 Arr::wrap($parameters) 处理成如图的数组,
又会被接下来的 foreach 处理成 [ 0 => 51]这样的数组.

所以实际和 return redirect()->route('users.show', $user->id);route('users.show', [$user->id]);效果相同.
file
file
最后结果就是 http://sample.test/users/51 也就是用户详情页

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!