Reputation: 110
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
Upvotes: 0
Views: 77
Reputation: 110
I've done as suggested and auth works as expected but something else broke. I have a modal in page for addding a new post but it doesnt work for some reasons i cannot see. In the posts controller i have the create(returns "posts.create" ) and store function to add a new post to 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");
}
And in the view create.blade.php i have
<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>
It looks like it doesn't go on the route it should
Auth::routes();
Route::get("/","PostsController@index")->middleware("auth");
Route::resource("posts", "PostsController")->middleware("auth");
Did i messed up on routes?
Edit:Nevermind, i've figured it out. Modal submit button had data-dismiss instead of data-toggle. That would cause the modal to be dismissed instead of closed after submitting.
Upvotes: 0
Reputation: 231
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!
Upvotes: 2