dourangdz750379 2018-10-25 02:54
浏览 56
已采纳

管理面板在Laravel中使用Session - 获取MethodNotAllowedHttpException

I'm trying to create a simple Admin Panel using session in Laravel. I don't plan on using Laravel authentication. I'm getting an error. I realize that its happening because of the get method in web.php file, I even changed it to post but still not working. Kindly help, I'm getting the below error. -

RouteCollection.php line 251 at RouteCollection->methodNotAllowed(array('GET', 'HEAD'))in RouteCollection.php line 238

Login.blade.php -

@php
    session_start();
    echo isset($_SESSION['login']);

    if(isset($_SESSION['login'])) {
        header('LOCATION:dashboard'); die();
    }
@endphp

<html>
<body>
    <h3>Login</h3>

@php
      if(isset($_POST['submit'])){
        $username = $_POST['username']; 
        $password = $_POST['password'];

        if($username === 'admin' && $password === 'password')
        {
            $_SESSION['login'] = true; header('LOCATION:dashboard'); die();

        } else {
            echo "Username and Password do not match";
        }

      }
 @endphp
    <form action="" method="post">

        Username:
        <input type="text"  id="username" name="username" required>


        Password:
        <input type="password" id="pwd" name="password" required>

        <button type="submit" name="submit">Login</button>
    </form>

</body>
</html>

Web.php -

Route::get('/admin', function () {
    return view('admin.login');
});

Route::get('/dashboard', function () {
    return view('admin.dash');
});

Dash.blade.php -

@php
    session_start();
    if(!isset($_SESSION['login'])) {
        header('LOCATION:admin'); die();
    }
@endphp
<!DOCTYPE html>
<html>
<body>
    <h3>Dashboard</h3>
</body>
</html>
  • 写回答

3条回答 默认 最新

  • doqs8936 2018-10-25 04:02
    关注

    Looks like you're trying to use the view for the initial get as well as the post on form submit. That's generally a bad practice and you're also using a lot of plain php outside of the framework, which isn't recommended.

    Nevertheless, you can try doing:

    Route::match(['get', 'post'], function () {
        return view('admin.login');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序