做我的第一个laravel项目我一直试图用laravel进行路由。 我想要的是当输入localhost但没有登录然后显示我的登录页面
这是我的登录页面的样子,注册与不同的字段/表单操作几乎相同
@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 /它会得到一个空白页面。看到页面的源代码,它似乎在布局之前@include一个来自create.blade.php的模态,即使在布局页面中没有模态,该模态存在于我的项目中,但它不是@included where
答案 0 :(得分:2)
有几种方法可以解决这个问题。
1。在DashboardController
的构造函数中引用'auth'中间件,如下所示:
class DashboardController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
...
然后,对此控制器发出的所有请求都绑定到“auth”中间件,该中间件在未经身份验证的用户尝试访问时将其重定向到/ login。
2。在routes/web.php
文件中:
Route::group(['middleware' => 'auth'], function() {
Route::get('/dashboard', 'DashboardController@index');
});
使用第二种方法,只有@index路由受'auth'中间件保护。
希望这会有所帮助。享受您的第一个Laravel项目!
答案 1 :(得分:0)
我按照建议做了,auth按预期工作,但其他东西坏了。 我在页面中有一个模态用于添加新帖子,但由于某些我无法看到的原因它不起作用。 在posts控制器中,我有create(返回&#34; posts.create&#34;)和store函数来向db添加新帖子。
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");
}
&#13;
在视图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>
&#13;
看起来它不应该继续它应该
的路线
Auth::routes();
Route::get("/","PostsController@index")->middleware("auth");
Route::resource("posts", "PostsController")->middleware("auth");
&#13;
我在路线上搞砸了吗?
编辑:没关系,我已经弄明白了。模态提交按钮具有数据关闭而不是数据切换。这将导致模式在提交后被解雇而不是关闭。