繁体   English   中英

刀片模板中的Laravel返回变量

[英]Laravel return variable in blade template

我有一条下面的路线。

Route::get('profile/{username}', 'ProfileController@index');

ProfileController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class ProfileController extends Controller
{
    function index() {
        view('profile.index');
    }
}

profile / index.blade.php

{username}

但是当我转到/profile/salep ,它不会回显用户名,这里缺少什么?

如果我将ProfileController更改为此(如下),则可以使用,但是PhpStorm对该视图说“无法到达的死角”。

class ProfileController extends Controller
{
    function index($username) {
        return $username;
        view('profile.index');
    }
}

我没有使用下面的结构(从文档中获取了它),因为我需要将变量传递给视图而不是返回,所以我想既需要返回又将其传递给视图。

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

您第二次尝试就快到了。

尝试这个:

class ProfileController extends Controller
{
    function index($username) {
        return view('profile.index')->with('username', $username);
    }
}

我解决了

routes.php

Route::get('profile/{username}', 'ProfileController@index');

ProfileController.php

class ProfileController extends Controller
{
    function index($username) {
        return view('profile.index')->with('username', $username);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM