簡體   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