【问题标题】:Laravel return variable in blade templateLaravel 在刀片模板中返回变量
【发布时间】:2015-08-16 09:07:42
【问题描述】:

我有如下路线。

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;
});

【问题讨论】:

    标签: php laravel-5.1


    【解决方案1】:

    第二次尝试时你就快到了。

    试试这个:

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

    【讨论】:

    • 我已经想通了,并在一分钟前添加了确切的答案,但是谢谢! :)
    【解决方案2】:

    我解决了。

    routes.php

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

    ProfileController.php

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

    【讨论】:

      猜你喜欢
      • 2014-12-08
      • 2018-05-08
      • 2015-05-06
      • 2013-05-06
      • 2017-02-08
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多