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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里