简体   繁体   English

会话未传递给视图Laravel

[英]Session not passing to the view laravel

I am trying to redirect to a route and pass data and I used with but no go and I also used to just simply set the sessions and then fetch them in the view but no go 我试图重定向到路由并传递数据,我曾经使用过但没有走过,而且我也曾经只是简单地设置会话,然后在视图中获取它们但没有走过

Controller code 控制器代码

if($socialUser) {

   Session::put('redirect', 'student-dashboard');
   Session::save();

   return redirect('/register/candidate');
}

View code 查看代码

<?php if(\Session::get('redirect')) {

        $redirect = session('redirect');
        echo $redirect;
        die();
    } 
?>  

But it never enters this if check and I need data on this view so any advise on this will be helpful. 但是,如果进行检查,它将永远不会输入此信息,并且我需要此视图上的数据,因此对此的任何建议都将有所帮助。

Please Note :- Session is available in the controller after being sent and I am using Laravel 5.6 请注意:-发送会话后控制器中可以使用会话,我正在使用Laravel 5.6

Have you tried the code where you then instead set $redirect like you ask the if statement: 您是否尝试过在代码中设置$ redirect的位置,就像询问if语句一样:

$redirect = \Session::get('redirect');

Also if you are using blade templates, you can write the php as; 另外,如果您使用刀片模板,则可以将php编写为:

@php
if (\Session::has('redirect'))
{
    $redirect = \Session::get('redirect');
    echo $redirect;
}
@endphp

The if (Session::has('redirect')) seems to be the proper way to check for whether or not a session exists, according to the Laravel 5.0 documentation, so I'm also leaving that there as a possible change that may help your code work. 根据Laravel 5.0文档, if (Session::has('redirect'))似乎是检查会话是否存在的正确方法,因此我也将其保留为可能的更改帮助您的代码工作。

I'm not sure u need to use Session::save(); 我不确定您是否需要使用Session::save(); for me its working without it. 对我来说,没有它的工作。 Try this one. 试试这个。

//controller
if($socialUser) {
   Session::put('redirect', 'student-dashboard');
   return redirect('/register/candidate');
}

//view
@php
    use Session; // or without it just using \Session
    if( Session::has('redirect') ){
      $redirect = Session::get('redirect');
      echo $redirect;
      die();
    }
@endphp

You can also try to use following syntax in controller 您也可以尝试在控制器中使用以下语法

 return redirect('/register/candidate')->with('redirect','student-dashboard');

And in view 并鉴于

@php 
   if(session('redirect')){
     //your code here
   }
@endphp

You can't redirect in a view. 您无法在视图中重定向。 Redirects have a range of HTTP status codes (3XX). 重定向具有一系列HTTP状态代码(3XX)。 If you've presented a view, then HTTP headers have already been sent (probably with a 200 OK status). 如果您提供了视图,则表明HTTP标头已经发送过(状态可能为200 OK)。

Instead, perform the redirect in your controller: 而是在控制器中执行重定向:

if ($socialUser) {
    return redirect('student-dashboard');
}

If you need to set the redirect URL in the view for some reason (ie you're passing it to a hidden input) then you'll need to do just that: pass it as a view parameter. 如果出于某种原因(例如,将其传递给隐藏的输入)需要在视图中设置重定向URL,则只需执行以下操作:将其作为视图参数传递。 Don't try setting it in the session. 不要尝试在会话中进行设置。 You won't see any session data set in your controller because the session data is only written after a response has been sent (ie after your view is rendered). 您不会在控制器中看到任何会话数据集,因为仅发送响应后(即,在渲染视图之后 )才写入会话数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM