doumouyi4039 2018-05-23 14:30
浏览 157

Laravel路由与身份验证

Doing my first laravel project I've stuck myself trying to make a routing with laravel. What i want is when entering localhost and not logged in then display my login page

This is how my login page looks like, register is almost the same with different fields/form action

@extends('layout.app')
@section('content')
    <style>
        body {
            background: url{{asset('img/bg.jpg')}} no-repeat center center fixed;
            -webkit-background-size: cover;
            -moz-background-size: cover;
            -o-background-size: cover;
            background-size: cover;
        }
    </style>
    <div class="container h-100 d-flex justify-content-center">
        <div class="row">
            <div class="col-lg-6">
                <div class="jumbotron">
                    <h1>WEB_NAME</h1>
                    <hr>
                    <h2>The place where your ideas may come real</h2>
                    <hr>
                    <p>Add your ideas and let others help you with your plans, vote other's ideas and discuss about
                        them</p>
                </div>
            </div>
            <div class="col-lg-6">
                <h1>Login below</h1>
                <form action="../index.php" method="post">
                    <div class="form-group">
                        <div class="col-xs-3 col-lg-6">
                            <label for="email">Email:</label>
                            <input type="email" class="form-control" name="email" id="email">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-xs-3 col-lg-6">
                            <label for="password">Password:</label>
                            <input type="password" class="form-control" name="password" id="password">
                        </div>
                    </div>

                    <p><input type="submit" name="submit" value="Login" class="btn btn-primary btn-lg" role="button">
                        <input type="reset" value="Register" class="btn btn-success btn-lg" role="button"
                               href="register.html">
                    </p>
                </form>
            </div>
        </div>
    </div>
@endsection

This is the app layout page

<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>{{config('app.name','SOCIAL IDEAS')}}</title>

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <!-- Styles -->
    <link href="{{ asset('vendor/bootstrap/css/bootstrap.min.css') }}" rel="stylesheet"/>
</head>
<body>
<div id="app">
    @include('inc.navbar')
    <div class="container">
        @yield('content')
    </div>

    <footer class="bg-dark">
        <div class="container">
            <p class="m-0 text-center text-white">Footer note</p>
        </div>
    </footer>
</div>
<script src="{{asset('vendor/jquery/jquery.js')}}"></script>
<script src="{{asset('vendor/bootstrap/js/bootstrap.min.js')}}"></script>
<script src="{{asset('js/bootstrap-notify.min.js')}}"></script>
<script src="{{asset('vendor/unisharp/laravel-ckeditor/ckeditor.js')}}"></script>
<script>
    CKEDITOR.replace('article-ckeditor');
</script>
</body>
</html>

Routing looks like this

Route::get('/', "PagesController@index");
Route::resource("posts", "PostsController");

Auth::routes();

Route::get('/dashboard', 'DashboardController@index');

How i am supposed to make the app work like i'm expecting: User enters website, if authenticated goes to page. If not then its redirected automatically to login page. After logged it it goes to same page as the one above This project would be a social network website. Users join, make a post, follow others and see other posts

The problem right now is the fact that when entering localhost/ it gets a blank page. Seeing source code of page it seems it does @include a modal from create.blade.php before layout even tho there is no modal in the layout page, that modal exists in my project but its not @included anywhere

  • 写回答

2条回答 默认 最新

  • duanjiwu0324 2018-05-23 15:13
    关注

    There are several ways to address this.

    1. Reference the 'auth' middleware in your DashboardController's constructor, like the following:

    class DashboardController extends Controller
    {
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('auth');
        }
    
    ...
    

    Then all requests made to this controller are bound to the 'auth' middleware, which redirects unauthenticated users to /login when they try to access it.

    2. In your routes/web.php file:

    Route::group(['middleware' => 'auth'], function() {
        Route::get('/dashboard', 'DashboardController@index');
    });
    

    With the second method, only the @index route would be protected by the 'auth' middleware.

    Hope this helps. Have fun with your first Laravel project!

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)