I am trying to 'redirect to' from one controller method to another through the route. However, I want to pass some data as well. I tried Session::get('name') but doesn't seem to work. This is what I tried:
public function before() {
return Redirect::to('later')->with('x', 'y');
}
public function later() {
dd( Session::get('x') ); // null
dd( $x ) // not working
}
My route is like classic:
Route::get('/later', 'TheController#later')->middleware('auth');
What am I missing?
Instead of Session::get('x') try with session('x') as below.You can check for it using if (session()->has('x'))
public function later() {
dd( session('x'));
}
Related
I'm encountering a problem where a redirect from one route to another is calling the targeted controller method twice. This question addresses a similar issue, but the OP passing a 301 status code was deemed to be the issue in the accepted answer, and I'm not specifying any status code. I'm also using the session state for parameters. The relevant code looks something like this:
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()
->action('SampleController#confirmUser')
->with([
'cvId' => $cvId,
'userId' => $user->id,
]);
}
public function confirmUser(Request $request) {
$cvId = session()->get('cvId');
$userId = session()->get('userId');
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Any ideas what could be causing this? I don't have any next middleware or any of the other possible causes that are suggested in related questions where controllers are executed twice.
Thanks for any help!
Have you tried passing values in parameters? Try the below code.
public function origin(Request $request) {
// Assume I have set variables $user and $cvId
return redirect()->action(
'SampleController#confirmUser', ['cvId' => $cvId, 'userId'=>$user->id]
);
}
public function confirmUser(Request $request) {
$cvId = $request->cvId;
$userId = $request->userId;
if (is_null($cvId) || is_null($userId)) {
// This is reached on the second time this is called, as
// the session variables aren't set the second time
return redirect('/home');
}
// We only see the view for fractions of a second before we are redirected home
return view('sample.confirmUser', compact('user', 'cvId'));
}
Why i have NotFoundHttpException???
My code is working fine, except for this route.
I have another route which looks like this and it's working fine.
I tried to remove (id) and write id in controller
function npa()
{
$news = Post::where('category_id', 28)->orderBy('created_at', 'desc')->paginate(12);
return view('npa', compact('news'));
}
Route::get('/npa/{id}', 'HomeController#npa');
Because your id is not optional in the route change your route to this:
Route::get('/npa/{id?}', 'HomeController#npa');
Pass Id to your route -> Route::get('/npa/{id}', 'HomeController#npa') as /npa/28
And change your function as
function npa($id)
{
$news = Post::where('category_id', $id)->orderBy('created_at', 'desc')->paginate(12);
return view('npa', compact('news'));
}
I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......
I have a route of GET type with some parameters. For example
Route::get('/my-route/{id}',array('uses'=>'myController#myAction'));
I want to check value of the parameter id and if this id=1 then redirect to another route else continue with it. What I am doing is like this
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
//What should I do here so my route works as before.
}
});
In else part I want my routes to myController#myAction along with parameters.
Thanks
You can do it as:
Route::get('/my-route/{id}',function($id){
if($id==1){
return Redirect::to(URL::route('my-another-route'));
}
else{
return app()->call(myController::class, ['id' => $id], 'myAction');
}
});
The easiest way to get this to work and route to your controller is put your Route back to the way it was and then the if statement to the top of your controller condition:
public function myAction() {
if ($id == 1) {
return Redirect::to(URL::route('my-another-route'));
}
to the top of your controller method.
Also, if you're only using uses => 'Controller#method' on with your route you can just do:
Route::get('/my-route/{id}','myController#myAction');
Hope this helps!
I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.