Jump to content

[Laravel] Can't pass variables to view


Recommended Posts

Hi everyone

I'm in learning laravel, really a newbie.

It's seems somehow I can't pass the variables from controllers to view.

So this is what I've got



Route::get('/' , "PagesController@link");
Route::get('pg' , "PagesController@link");
Route::get('pg/{link}' , 'PagesController@link');


    public function link($link = 'index') {
// option 1
        return "--->" . $link;
// option 2
        return View::make('pages.index' , array('link' => $link));
// option 3
        return View::make('pages.index')->with('link', $link);
// option 4
        return View::make('pages.index' , compact('link'));



    @parent - Index file

    <header>This is the {{ $link }}</header>
    <p>And this is the main body of {{ $link }} file</p> 


<!doctype html>
    <meta charset="utf-8">
        Lar4 Test 
    {{ HTML::style('css/frontend.css') }}

    <header id="header">
        <div id='banner'>
            {{ HTML::link('pg' , 'Home') }}
            {{ HTML::link('pg/about' , 'About Us') }}



So if I browse to <site>/pg/sometext only option 1 will work (shows "---->sometext"), all others will thrown an error "Undefined variable: link".


If I comment both {{ $link }}, both layout and index views are successfully rendered.


Any idea of what am I doing wrong?



Link to post
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.