I have this code in a project with Laravel 5:
return back()->with('msg_ok','successfully sent');
The param msg_ok is pushed in Session, but I´m not want use session params, I want pass the msg_ok parameter as variable.
For example I want print this en my blade file:
{{ $msg_ok }}
Laravel back() function will always return data in a Session. Normally you can't return variable with back() function. You have to use view() function for that.
Alternate Solution
You can use keep() function for storing data in session like variable. Which will not be flushed after refresh.
e.g
$request->session()->keep(['username', 'email']);
and then get data with key.
Laravel back() will redirect you back from the form where it is submitted,
and back()->with('msg_ok', 'successfully..');
if you place an item inside back()->with(); it will automatically place the value
inside session.
You can use return redirect()->to('url/path?msg_ok=successfully') add the query string parameter to url to pass it as variable.
and then on the controller where you have been redirected inject the Reqest class to your method
e.g
public function index(Request $request) {
$msg_ok = $request->get('msg_ok', '');
return view('view.blade', compact('msg_ok'));
}
Place the variable string inside the compact to make it accessible to your view.
You can just use Session::get('msg_ok') in your blade file. Or pass the variable to the view explicitly, in controller, like
return view()->make('view', ['msg_ok' => session()->get('msg_ok')]);
Related
I have setup a system that relies on the routes.php and I have now come across a problem which is that I need to pass a variable to a route which redirects to a controller and receive that variable in the controller. Is this possible. Thanks
Main Controller
return redirect()->route('ROUTENAME')->with("Variable", Array);// Variable has to come from here
web.php
Route::get('ROUTE', "FUNCTION")->name('ROUTENAME');//Need to receive the array here and pass it on
RecieveFunction
$Variable;//This was passed from Main Controller and forwarded from the route
You can use session()->get('Variable'); in the RecieveFunction
You could do this to receive data:
use Illuminate\Http\Request;
Route::get('ROUTE', static function(Request $request){
...
$variable = $request->session()->get('Variable'));
# Delete the data if you don't to keep it in session
$request->session()->forget('Variable');
...
} )->name('ROUTENAME');
OR if you want to pass it to Controller
use App\Http\Controllers\ReceivingController;
Route::get('ROUTE', [ReceivingController::class, 'function_name'] )->name('ROUTENAME');
# ReceivingController
public function function_name(Request $request)
{
...
$variable = $request->session()->get('Variable'));
# Delete the data if you don't to keep it in session
$request->session()->forget('Variable');
...
}
I am using Zend Framework 3 and I have a controller which routes to a view when hasIdentity is true (works)
How do I include a variable (e.g. $myVar) into the redirect so I can use it in my view?
if ($this->authService->hasIdentity()) {
$myVar = "some data I want to pass to my view";
return $this->redirect()->toRoute('auth/welcome');
}
And how do I access that in my view?
There are several ways:
You can add it as query arguments in your redirect URL
return $this->redirect()->toRoute('auth/welcome', [
'message' => 'some data I want to pass to my view',
]);
You can store the value in session, redirect, retrieve from session and pass to the view.
Use flashMessenger view helper (extension of point 2).
Hope this works…
I am trying to first post my form to my method that returns a view to preview the post that has been done. When you get to the new view of the preview i want to be able to use a link to go back to the previous page with all form inputs already filled in.
I have tried doing this with setting another url and route to the link where the route triggers a method in my controller with the return back() call. This does not seem to work and i guess it is because it is outside my method with the $request.
So i am wondering how it would be possible to achieve what i want to do. Im using Laravel 5.5
EDIT
Here is my current code:
After the form is posted, you are sent to this method:
public function store(Request $request)
{
$preview = new Preview();
$preview->post_title = $request->title;
$preview->save();
$previewData = Preview::latest('id')->first();
return view('pages.preview_ad')->with('previewData', $previewData);
}
In the 'preview_ad' view, i want to have a link that routes to another method that triggers
return back()->withInput();
Although when i try to do this with the code below, it does not return the previous page with the inputs which the code above should. It works properly if i run it inside the 'store' method above, but not in the other method.
Route::get('/Skapa-annons/Tillbaka', 'PreviewsController#go_back');
-
public function go_back()
{
return back()->withInput();
}
I can access cookie in controller then pass it to view
//HomeController.php
public function index(Request $request)
{
$name = Cookie::get('name');
return view('index', ['name'=> $name]);
}
But I want to write a small control (widget) that can fetch data from cookie without concern of parent controller. For example, header, footer widgets could fetch its own data without main page controller knowing which data is needed.
I can query the data from database by using View Composer. But, how can I access data from view in the request cookie ?
Using static function with defining namespace and etc is a not safe.
Cuz maybe in next versions of framework this namespace can change.
It's better to use helper functions.
{{ request()->cookie('laravel_session') }}
or
{{ cookie('laravel_session') }}
Tested on working app with Laravel 5.2
You can use {{ Cookie::get('laravel_session') }} to print out the cookie inside your view.
You can use
{{\Illuminate\Support\Facades\Cookie::get('laravel_session')}}
in a blade template
You can use:
$response = new \Illuminate\Http\Response(view('your_view'));
$response->withCookie(cookie('cookieName' , 'cookieValue' , expire));
return $response;
or
\Cookie::queue('cookieName', 'cookieValue', expire);
return view('your_view');
I've used both of them.
I have just started using laravel and currently using the version 5.2.
I am using two forms to get the data. First form in first view and second form in second view. I need to figure out a way to tell the second form, the form id of the first one, which the second form will be linked to.
I know this can be done by passing the values using the URL. But I lack the knowledge of the correct syntax.
1- How to send data while redirecting?
2- How should the route look like?
3- How to access that value in the second view, in order to pass that value when the second form submits?
I have googled a lot about this but couldn't understand those advanced syntax.
Any help is appreciated.
Thanks in advance.
This is the code in controller:
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
Session::flash('values',$request->azauj_id);
return redirect('/add/requirement');
}
public function getCreateRequirement(Request $request){
$att = Session::get('value');
Session::flash('value',$att);
return view('req');
}
public function postCreateRequirement(Request $request){
dd(Session::get('value'));
}
The forms are plain html forms with post methods of submission
When I use dd(Session::get('value'));, i get null. It means that the value is not being passed. To the postCreateRequirement method which is called when the second form is submitted.
Below are the routes.
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
1- How to send data while redirecting?
You can simply pass data with your redirect using the ->with() method which creates an session that will only appear on the next page more here
Example:
Say you want to send a status down to your view you add the with to your redirect:
// Where you are redirecting to
redirect("/otherRoute")->with("status", "Profile updated!");
// session name // data
Now you simply check if the session exist and echo it out:
// If the session does not exist it will return false and not create it
#if (session("status"))
<div class="alert alert-success">
// echo out the session
{{ session("status") }}
</div>
#endif
2- How should the route look like?
Routes should be defined in the routes.php file located in the http directory assuming you are posting the data you should make the routes and connect them to your controller like so:
//For Add Profile Page
Route::get('/add', 'ProfileController#getCreateProfile');
//For Add Profile Form Submission
Route::post('/add', 'ProfileController#postCreateProfile');
//For Add Requirements Page
Route::get('/add/requirement', 'ProfileController#getCreateRequirement');
//For Add Requirements Form Submission
Route::post('/add/requirement', 'ProfileController#postCreateRequirement');
3- How to access that value in the second view, in order to pass that value when the second form submits?
You could simply use the ->with() method in your redirect
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
return redirect('/add/requirement')->with("value",$request->azauj_id);
}
Get the value
public function getCreateRequirement(Request $request){
$value = session("value");
// compact it and trow it in an input to pass it trough to the last controller method
return view('req');
}
public function postCreateRequirement(Request $request){
$request->get("value");
}
OR
Create a global session and flush it afterwards
public function postCreateProfile(Request $request){
//Adding attributes from $request to $profile
$profile->save();
session("value",$request->azauj_id);
return redirect('/add/requirement');
}
Get the value
public function getCreateRequirement(Request $request){
return view('req');
}
public function postCreateRequirement(Request $request){
$value = session("value");
$request->session()->forget("value");
}
Note: Laravel does not flush sessions when somone logs out these will remain if not flushed by hand or using the method $request->session()->flush();