سؤال

i'm a laravel newbie and i'm trying to pass a string to a view, from a controller. As i understand it, it should be something like this:

return Redirect::route('home')->with('global','Hi!');

I'm trying to redirect a user to 'home', but have the string 'Hi!' show up before everything else. 'home' is a route that renders a view 'home.blade.php'.

As it is, the user is redirected, no errors, but the string "Hi!" doesn't show up on the 'home ' view. I am thinking i need something on the 'home' blade template to tell laravel where to put the string "Hi!", but what should that be?

هل كانت مفيدة؟

المحلول

Without knowing more about your code it's hard to say exactly what to do, but you should be aware that Redirect::route()->with() is NOT the same as View::make()->with(). View::make()->with('var', 'value') passes a variable called var to the view, whereas Redirect::route()->with('var', 'value') sets up a session flash variable.

In order to access 'global' in home.blade.php you need to find it in session:

{{{ Session::get('global') }}}

Incidentally, to get an actual 'global' view variable you need View::share('var', 'value'), however this will not work over a redirect, only for the current request.

نصائح أخرى

I think what you're looking for is

Session::flash('global', 'Hi!');

The "flash()" method puts data into the session, but for only one request. After that it's deleted, which in this case seems like what you want. See more about this in the docs! http://laravel.com/docs/session#flash-data

Once you've flashed that data to the session, you can then return Redirect::view('home') just like you are already doing, and on the home view use {{ Session::get('global') }} just as alexrussell suggested.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top