【问题标题】:How to fix undefined variable when my route is slash using laravel?当我的路线使用 laravel 斜线时如何修复未定义的变量?
【发布时间】:2020-04-25 14:05:02
【问题描述】:

您好开发人员,我的控制器有问题,当我尝试访问斜杠路由时,我收到了这个错误 get_the_recent_post (View: C:\xampp\htdocs\staging_project\resources\views\index.blade.php) 那个错误当我的帐户注销时发生。但是,如果我的帐户已登录,则没有发现错误。

为了更好地理解,如果我访问此链接 http://localhost:8000/ 并且我的帐户已关闭,我会收到此错误 Undefined variable: get_the_recent_post, 但是如果我访问我的帐户我在数据库中获取的数据正在显示。

目标:如果我的路线是http://localhost:8000/,我的输出必须与http://localhost:8000/index上的相同

我的路线:

Route::get('/', function () {
  return view('/index');
});

Route::get('/index', 'HomeController@index')->name('index');

HomeController:

   /**
     * Create a new controller instance.
     *
     * @return void
     */
    // public function __construct()
    // {
    //     $this->middleware('auth');
    // }
/**
 * Show the application dashboard.
 *
 * @return \Illuminate\Contracts\Support\Renderable
 */

public function index()
{

    $get_the_recent_post = DB::select('SELECT content_id,content_title,content_desc,when_created,content_author FROM icweb_content WHERE content_status = ? AND MONTH(when_created) = MONTH(CURDATE()) ORDER BY when_created DESC LIMIT 3',[

        'Approved'

    ]);

    $get_the_upcoming_event = DB::select('SELECT ic_content.content_id,content_title,content_desc,content_author,start_event,end_event,when_created FROM icweb_content as ic_content 

        WHERE content_status = ? AND  start_event = CURDATE() AND end_event >= CURDATE() ORDER BY when_created DESC LIMIT 3
        ',[

            'Approved'

        ]);

    return view('/index')
    ->with('get_the_recent_post',$get_the_recent_post)
    ->with('get_the_upcoming_event',$get_the_upcoming_event);

}

index.blade.php:

    @foreach($get_the_recent_post as $data_recent_post)
        <a href="" style="text-decoration: none;">
          <div style="line-height: 2px;">
            <h5 class="card-title" style="font-weight: 500; font-size:13px;  color:black;">{{$data_recent_post->content_title}}</h5>
            <p class="card-text" style="color:#757a91; font-size:12px; font-weight: 500;">{{strip_tags($data_recent_post->content_desc)}} </p>
          </div>
        </a>
   @endforeach

【问题讨论】:

  • 我认为您还需要在“/”路由中调用“HomeController@index”。与您在“/index”路由中调用的相同。
  • @SohilChamadia 我试过了,但还是不行
  • 在您的“/”操作中,您只是在加载没有数据的视图页面,您也需要加载数据
  • 否则你需要这样做 Route::get('/', 'HomeController@index')->name('index2');
  • 当您在“/”路由中调用“HomeController@index”时,您面临什么问题?

标签: php laravel


【解决方案1】:

只需像下面分享的代码 sn-p 一样更改您的路线。

Route::get('/', function () {
  return redirect()->route('index');
});

【讨论】:

    【解决方案2】:

    改变你的路线然后点击。

    示例:- Route::get('/', 'HomeController@index');

    如果这个不工作,那么需要调试。

    【讨论】:

    • 如果我尝试这样做,页面没有响应
    • 嗨,阿米尔汗,当我试图把它说出来时。我的重定向在登录中。如果用户未通过身份验证。如何使如果没有经过身份验证,它将转到反斜杠的路线。?
    • 使用 dd('check');检查函数是否正在调用。如果页面没有响应,则可能是查询问题。
    • @SoulAiker 如果您在没有登录用户的情况下进行重定向,请在 Auth 中间件之外编写您的路由并检查刀片文件上的 isset if(isset($get_the_recent_post)){ foreach(....) { ----- } } 其他 { }
    【解决方案3】:

    解决我的问题:

    我把这个函数注释掉

    // public function __construct()
    // {
    //     $this->middleware('auth');
    // }
    

    【讨论】:

    • 好的,这是 laravel 的 auth 中间件。所以如果你不想在登录前不访问任何控制器给用户,那么你需要使用它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-15
    • 2023-03-29
    • 1970-01-01
    • 2017-03-31
    • 1970-01-01
    • 2016-01-24
    • 2020-05-03
    • 2016-04-02
    相关资源
    最近更新 更多