[英]Laravel routing with authentification
在做我的第一個laravel項目時,我一直努力嘗試與laravel進行路由。 我想要的是進入本地主機但未登錄時顯示我的登錄頁面
這是我的登錄頁面的樣子,使用不同的字段/表單操作注冊幾乎相同
@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
這是應用程序布局頁面
<!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>
路由看起來像這樣
Route::get('/', "PagesController@index");
Route::resource("posts", "PostsController");
Auth::routes();
Route::get('/dashboard', 'DashboardController@index');
我應該如何使應用程序像我期望的那樣工作:如果通過身份驗證,請進入網站。 如果不是,則其自動重定向到登錄頁面。 登錄后,它會轉到與上一頁相同的頁面。該項目將是一個社交網站。 用戶加入,發布信息,關注其他人並查看其他信息
現在的問題是這樣的事實,當輸入localhost /時,它會得到一個空白頁。 看到頁面的源代碼,似乎它在布局之前就已經@ create.blade.php中包含了一個模態,甚至在布局頁面中沒有模態,即該模態存在於我的項目中,但是在任何地方都沒有@include模態。
有幾種解決方法。
1.在DashboardController
的構造函數中引用“ auth”中間件,如下所示:
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
...
然后,對此控制器發出的所有請求都將綁定到“身份驗證”中間件,該中間件會將未經身份驗證的用戶重定向到/ login,以便他們嘗試訪問它。
2.在您的routes/web.php
文件中:
Route::group(['middleware' => 'auth'], function() {
Route::get('/dashboard', 'DashboardController@index');
});
使用第二種方法,只有@index路由將受“ auth”中間件保護。
希望這可以幫助。 享受您的第一個Laravel項目的樂趣!
我已經按照建議完成了工作,並且auth可以按預期工作,但是其他事情壞了。 我在頁面中有一個用於添加新帖子的模式,但是由於某些原因我看不到它不起作用。 在posts控制器中,我具有create(returns“ posts.create”)和store函數來向db添加新的post。
public function store(Request $request) { $this->validate($request, [ 'title' => 'required|max:255', 'description' => 'required|max:2000' ]); $post = new Post; $post->title = $request->input("title"); $post->description = $request->input("description"); $post->author_id = Auth::user()->id; $post->save(); return redirect("/posts"); }
在視圖create.blade.php中,我有
<div class="modal fade " id="postsmodal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog modal-lg" role="document"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Create new Post</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">×</span> </button> </div> <div class="modal-body"> <div class="row"> <div class="col-lg-4"> {!! Form::open(['action' => 'PostsController@store' , "method" => "POST"]) !!} <img class="img-fluid" src="http://placehold.it/250x150" alt=""> <div class="form-group"> <label for="exampleInputFile">Picuture</label> <input type="file" class="form-control-file" id="exampleInputFile" name="photo" aria-describedby="fileHelp"> </div> <div class="form-group"> {{Form::label('Title', "Post Title")}} {{Form::text('title', '',['class' => 'form-control'])}} </div> </div> <div class="col-lg-8"> <div class="form-group"> {{Form::label('description',"Post Description")}} {{Form::textarea('description', '', ['id' => 'article-ckeditor','class' => 'form-control', 'rows' => '3'])}} </div> </div> </div> </div> <div class="modal-footer"> <div class="mr-auto"> Created by: {!! Auth::user()->first_name," ", Auth::user()->last_name , " on ", \\Carbon\\Carbon::now()->toDateString() !!} </div> {!! Form::submit("Submit", ['class' => 'btn btn-primary', 'data-dismiss' => 'modal']) !!} <input type="button" name="reset" value="Close" class="btn btn-primary"> </div> {!! Form::close() !!} </div> </div> </div>
看來它沒有走應該走的路
Auth::routes(); Route::get("/","PostsController@index")->middleware("auth"); Route::resource("posts", "PostsController")->middleware("auth");
我搞砸了路線嗎?
編輯:沒關系,我想通了。 模式提交按鈕具有數據關閉功能,而不是數據切換功能。 這將導致模式被取消,而不是在提交后關閉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.