Jump to content

[Laravel] Can't pass variables to view


XicoXperto

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

 

config/route.php

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

controllers/PagesController.php

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

views/pages/index.blade.php

@extends('layouts.frontend')

@section('title')
    @parent - Index file
@stop

@section('content')
<article> 
    <header>This is the {{ $link }}</header>
    <p>And this is the main body of {{ $link }} file</p> 
</article>
@stop

views/pages/layouts/frontend.blade.php

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

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

    <aside>
    </aside>

    <footer>
    </footer>
    
</body>
</html>

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 comment
https://forums.phpfreaks.com/topic/278290-laravel-cant-pass-variables-to-view/
Share on other sites

Archived

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.