2
votes

I'm trying to learn Laravel 4 and reading the book called Laravel: code bright

I'm trying to pass array data from routes to view but I get this error

ErrorException Undefined variable: squirrel

Codes are like these. Same as in the book.

url localhost/test

routes.php

<?php
    Route::get('/{squirrel}', function() {

        $data['squirrel'] = $squirrel;

        return View::make('simple', $data);
    });
?>

simple.php

<!DOCTYPE html>
    <html lang="eng">
    <head>
            <meta charset="UTF-8">
            <title>Views!</title>
    </head>
    <body>
            <p>I  wish I were a <?php echo $squirrel; ?> squirrel!</p>
    </body>
    </html>
2

2 Answers

4
votes

You have to get the variable like this:

<?php
    Route::get('/{squirrel}', function($squirrel) {

        $data['squirrel'] = $squirrel;

        return View::make('simple', $data);
    });
0
votes

I was having same problem, but error was still ocurring. I just realized here that If I tried to go to / folder, then the variables was not found. In this example, you must go /foo to get no errors.

I think that in order to be optional, I would have to add the optional ? thing and the $squirrel = null, similar to the previous example.

It took me a few minutes to realized that, might be helpfull for anyone ;)

[]s