dtqysxw4659 2016-01-10 02:38
浏览 35
已采纳

将数据传递到视图laravel

Just getting to mvc frameworks and im trying to pass data to my view using blade template engine.

here's my routes file

Route::get('/', 'PagesController@index');

my PagesController

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;

class PagesController extends Controller {

    public function index() {

        return view('index');

        $url = 'example.com';
    }

    public function about() {


        return 'hello';


    }

}

my index.blade.php file

<h1>{{ $url }}</h1>

I get the Undefined variable: url error message. What am i doing wrong?

  • 写回答

2条回答 默认 最新

  • doutuo8800 2016-01-10 02:42
    关注

    Try this

    public function index() {
       $url = 'example.com';
       return view('index', ['url' => $url]); 
    }
    

    Initialize the variable and send it as a parameter to the view.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?