I'm using Laravel 5.2 and I'm trying to get the error message to show when I attempt to login with the wrong username and password combination, so when I do that I don't get an error message showing. I'm not sure where I'm going wrong.
My controler
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
public function login(Request $request)
{
if(Auth::attempt(array('name' => Input::get('name'), 'password' => Input::get('password'))))
{
return redirect()->route('dashboard');
}else{
return redirect()->route('home')
->with('message', 'Your username and password combination is wrong')
->withInput();
}
}
}
my index.blade.php
#if ($errors->any())
{{ implode('', $errors->all('<div>:message</div>')) }}
#endif
<div class="form-wrapper">
<div class="login-header">
<h1>Login</h1>
</div>
<div class="form_input">
{{ Form::open(array('url' => 'admin/')) }}
<div class="form_group">
{{ Form::label('name', 'Name') }}
{{ Form::text('name', '' , array("class" => "form-control")) }}
</div>
<div class="form_group password-section">
{{ Form::label('password', 'Password') }}
{{ Form::password('password', array("class" => "form-control")) }}
</div>
<div class="form_group submit_button">
{{ Form::submit('Submit', array("class" =>"btn btn-info submit", "role" => "button")) }}
</div>
{{ Form::close() }}
</div>
</div>
My routes.php
Route::group(['middleware' => 'web'], function()
{
Route::get('admin/', [
'uses' => 'HomeController#index',
'as' => 'home'
]);
Route::post('/signin', [
'uses' => 'HomeController#login',
'as' => 'Login'
]);
});
use Route::group(['middleware' => 'auth'], function() in routes
Related
I'm trying to set the post admin section. The mission for that section is to show all articles that belong to logged user. The way I'm doing for tags and categories is working correct (tags and categories doesn't need to be filtered for any user). The post page works correctly show the owned post for logged user, but the problem is that the user can't edit or show any post and trying to store a new post redirects to 403 page. I'm confuse by the error and I don't have any solution. I appreciate some help.
PostModel
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'user_id', 'category_id', 'name', 'slug', 'excerpt', 'body', 'status', 'file'
];
public function category()
{
return $this->belongsTo(Category::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
PostController
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Post;
use App\Models\Category;
use App\Models\Tag;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\PostStoreRequest;
use App\Http\Requests\PostUpdateRequest;
use Illuminate\Support\Facades\Storage;
class PostController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$posts = Post::orderBy('id', 'DESC')
->where('user_id', auth()->user()->id)
->paginate();
return view('admin.posts.index', compact('posts'));
}
public function create()
{
$categories = Category::orderBy('name', 'ASC')->pluck('name', 'id');
$tags = Tag::orderBy('name', 'ASC')->get();
return view('admin.posts.create', compact('categories', 'tags'));
}
public function store(PostStoreRequest $request)
{
$post = Post::create($request->all());
$this->authorize('pass', $post);
//IMAGE
if($request->file('image')){
$path = Storage::disk('public')->put('image', $request->file('image'));
$post->fill(['file' => asset($path)])->save();
}
//TAGS
$post->tags()->attach($request->get('tags'));
return redirect()->route('posts.edit', $post->id)->with('info', 'Success');
}
public function show($id)
{
$post = Post::find($id);
$this->authorize('pass', $post);
return view('admin.posts.show', compact('post'));
}
public function edit($id)
{
$categories = Category::orderBy('name', 'ASC')->pluck('name', 'id');
$tags = Tag::orderBy('name', 'ASC')->get();
$post = Post::find($id);
$this->authorize('pass', $post);
return view('admin.posts.edit', compact('post', 'categories', 'tags'));
}
public function update(PostUpdateRequest $request, $id)
{
$post = Post::find($id);
$this->authorize('pass', $post);
$post->fill($request->all())->save();
//IMAGE
if($request->file('image')){
$path = Storage::disk('public')->put('image', $request->file('image'));
$post->fill(['file' => asset($path)])->save();
}
//TAGS
$post->tags()->sync($request->get('tags'));
return redirect()->route('posts.edit', $post->id)->with('info', 'Success');
}
public function destroy($id)
{
$post = Post::find($id)->delete();
$this->authorize('pass', $post);
return back()->with('info', 'Deleted');
}
}
PostUpdateRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostUpdateRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [
'name' => 'required',
'slug' => 'required|unique:posts,slug,' . $this->post,
'user_id' => 'required|integer',
'category_id' => 'required|integer',
'tags' => 'required|array',
'body' => 'required',
'status' => 'required|in:DRAFT,PUBLISHED',
];
if($this->get('image'))
$rules = array_merge($rules, ['image' => 'mimes:jpg,jpeg,png']);
return $rules;
}
}
PostStoreRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class PostStoreRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [
'name' => 'required',
'slug' => 'required|unique:posts,slug',
'user_id' => 'required|integer',
'category_id' => 'required|integer',
'tags' => 'required|array',
'body' => 'required',
'status' => 'required|in:DRAFT,PUBLISHED',
];
if($this->get('image'))
$rules = array_merge($rules, ['image' => 'mimes:jpg,jpeg,png']);
return $rules;
}
}
And for example, the post.edit
#extends('admin.admin')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="card">
<div class="card-header">
{{ __('Editar artÃculo') }}
</div>
<div class="card-body">
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
#include('admin.posts.partials.form')
{!! Form::close() !!}
</div>
</div>
</div>
</div>
</div>
#endsection
And finally the form
<div class="form-group">
{{ Form::hidden('user_id', auth()->user()->id) }}
</div>
<div class="form-group">
{{ Form::label('category_id', 'Category') }}
{{ Form::select('category_id', $categories, null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::label('name', 'Tag name') }}
{{ Form::text('name', null, ['class' => 'form-control', 'id' => 'name']) }}
</div>
<div class="form-group">
{{ Form::label('slug', 'URL friendly') }}
{{ Form::text('slug', null, ['class' => 'form-control', 'id' => 'slug']) }}
</div>
<div class="form-group">
{{ Form::label('image', 'Image') }}
{{ Form::file('image') }}
</div>
<div class="form-group">
{{ Form::label('slug', 'State') }}
<label>
{{ Form::radio('status', 'PUBLISHED') }} Published
</label>
<label>
{{ Form::radio('status', 'DRAFT') }} Draft
</label>
</div>
<div class="form-group">
{{ Form::label('tags', 'Tags') }}
<div>
#foreach($tags as $tag)
<label>
{{ Form::checkbox('tags[]', $tag->id) }} {{ $tag->name }}
</label>
#endforeach
</div>
</div>
<div class="form-group">
{{ Form::label('excerpt', 'Excerpt') }}
{{ Form::textarea('excerpt', null, ['class' => 'form-control', 'rows' => '2']) }}
</div>
<div class="form-group">
{{ Form::label('body', 'Description') }}
{{ Form::textarea('body', null, ['class' => 'form-control']) }}
</div>
<div class="form-group">
{{ Form::submit('Guardar', ['class' => 'btn btn-sm btn-primary']) }}
</div>
#section('scripts')
<script src="{{ asset('components/stringToSlug/jquery.stringToSlug.min.js') }}"></script>
<script>
$(document).ready(function(){
$("#name, #slug").stringToSlug({
callback: function(text){
$('#slug').val(text);
}
});
});
</script>
#endsection
Sorry for the extension but it was necessary to explain the problem.
The authorize() method in controller method looks for a corresponding policy. If Laravel can't find the corresponding policy it throws unauthenticated exception.
So in this case $this->authorize('pass', $post), expects to find a PostPolicy class, otherwise it will throw unauthorized exception which is converted to 403 redirect by middleware.
Read more about Policies https://laravel.com/docs/8.x/authorization#creating-policies.
Note: When using FormRequest to handle validation, authorization can be done in FormRequest and there's not need to duplicate authorization in controller method
I am creating a contact form using bootstrap3 build on Laravel 5.4. When I click the submit button, I expect an email to be sent to my inbox or if there are errors they should be validated on the back end and errors displayed at the top of the form. I am using Laravel collective to build the form, when I fill the form and click the submit button, the page only reloads and no validation happens or in case of correct input no email is sent. Please assist?
Form Section
<div class="col-sm-4 wow animated fadeInLeft">
<div id="success" class="col-sm-12">
#if(Session::has('success'))
<span class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</span>
#endif
#if(count($errors) > 0)
<span class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</span>
#endif
</div>
{!! Form::open(array('route' => 'index.post', 'method' => 'POST','class' => 'contact-form')) !!}
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => ''))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => ''))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Routes File
Route::post('/', 'PagesController#postIndex') ->name('index.post');
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
PagesController
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#pwebk.com');
});
Session::flash('success', 'Hello '.$name.', Your Form was successfully sent');
return redirect()->route('pages.index');
}
public function getIndex(){
return view('pages.welcome');
}
i have web site make in Laravel 5.4. I have contact form and try to send mail but when send i got in mail this data
Name: {{ $name }}
Email: {{ $email }}
Message: {{ $message1 }}
In laravel 5.1 I got the data but in Laravel 5.4 I can not pass data.
My web.php
Route::post('mailContact', 'SiteController#postEmailContact');
My contorller:
protected function postEmailContact() {
Mail::send('requestContact', array(
'name' =>Input::get("name"),
'email' =>Input::get("email"),
'message1' =>Input::get("message1")
), function ($message) {
$message->from('myMail#gmail.com', 'Contact');
$message->to('yourMail#gmail.com')->subject('Contact');
});
return redirect('/');
}
and my requestContact.blade.php
Name: {{ $name }}
Email: {{ $email }}
Message: {{ $message1 }}
and contact.blade.php
{!! Form::open(array('url' => 'mailContact','class'=>'form-group')) !!}
<div id="content-page" class="content group">
<div class="hentry group">
<div class="usermessagea"></div>
<label for="name-contact-us">
Name
</label>
<div class="input-prepend"> {!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'ime')) !!}</div>
<div class="msg-error"></div>
<label for="email-contact-us">
Email
</label>
<div class="input-prepend"> {!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'email')) !!}</div>
<div class="msg-error"></div>
<label for="message-contact-us">
Message
</label>
<div class="input-prepend"> {!! Form::textarea('message1', null,
array( 'placeholder'=>'message',
'class'=>'form-control'
)) !!}</div>
</br>
{!! Form::submit('send' , array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
Any idea? How to pass data?
You should try this:
Please change email field
{!! Form::text('name', null, array('class' => 'form-control','placeholder' => 'email')) !!}
to:
{!! Form::text('email', null, array('class' => 'form-control','placeholder' => 'email')) !!}
Updated answer
protected function postEmailContact() {
$data = array(
'name' =>Input::get("name"),
'email' =>Input::get("email"),
'message1' =>Input::get("message1")
);
Mail::send('requestContact',$data, function ($message) {
$message->from('myMail#gmail.com', 'Contact');
$message->to('yourMail#gmail.com')->subject('Contact');
});
return redirect('/');
}
Follow 3 step only
1] configure in .evn file at root dir. as above
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=username#gmail.com
MAIL_PASSWORD=******
MAIL_ENCRYPTION=tls
2]create controller
use Mail;
class mailController extends Controller
{
public function send(){
Mail::send(
['text' => 'post.mail'], //e.g post/mail.blade.php <view file mentioned here>
['name' => 'Name'],
function($message){
$message->to('username#gmail.com','To username');
$message->subject('test email yagnesh');
$message->from('username#gmail.com','from username');
}
);
}
}
AND create view file <post/mail.blade.php> set this name
3] run command at root dir. to Restart server <php artisan serve>
And
U can allow google less security at [https://www.google.com/settings/security/lesssecureapps][1]
Just enabled
4] create Route
//for send mail
Route::get('/send','mailController#send');
and run 'send' keyword in your url.
For more visit <https://www.youtube.com/watch?v=a08ouL3wjjQ&list=PLe30vg_FG4OQz1yZq0z19ZuWD_C3MZbA4&index=26>
Good luck!!!
I have got a contact form that is build using Laravel 5.4, parsley.js and Bootstrap 3, it works fine but it does not display a success message at the top of the form on successful delivery of a message or display any errors in case there is an error. Please assist?
Contoller
public function postIndex(Request $request){
$this->validate($request, array(
'name' => 'required|min:10',
'email' => 'required|email',
'message' => 'required|min:100'
));
$name = $request->name;
$data = array(
'name' => $request->name,
'email' => $request->email,
'bodymessage' => $request->message
);
Mail::send('emails.contact', $data, function($message) use ($data) {
$message->from($data['email']);
$message->to('info#kapsol.com');
});
Session::flash('success', 'Hello $name, Your Form was successfully sent');
return redirect()->route('pages.index');
}
Index.blade.php
<div class="col-sm-4">
{!! Form::open(array('route' => 'index.post', 'class' => 'contact-form', 'data-parsley-validate' => '')) !!}
<div id="success">
<div class="col-sm-12">
#if(Session::has('success'))
<div class="alert alert-success" role="alert">
<strong> Success: </strong> {{ Session::get('success') }}
</div>
#endif
#if(count($errors) > 0)
<div class="alert alert-danger" role="alert">
<strong> Errors: </strong>
<ul>
#foreach($errors->all() as $error)
<li> {{ $error }} </li>
#endforeach
</ul>
</div>
#endif
</div>
</div>
{{ Form::text('name', null, array( 'placeholder' => 'Name...', 'class' => 'input', 'required' => '', 'minlength' => '10'))}}
{{ Form::email('email', null, array('placeholder' => 'Email Address...','class' => 'input', 'required' => '', 'type' => 'email'))}}
{{ Form::textarea('message', null, array('placeholder' => 'Message...', 'class' => '', 'required' => 'input', 'minlength' => '100')) }}
{{ Form::submit('Submit') }}
{!! Form::close() !!}
</div>
Route
Route::get('/', 'PagesController#getIndex') ->name('pages.index');
Route::post('/', 'PagesController#postIndex') ->name('index.post');
You are redirecting to the same route your form is posting. I assume that the route that render the view is a GET route and have another name, you should redirect to that route or the route that renders index.blade.php
This is why you have the with() method to chain the route() method on. So rather than use Session:flash(), you can simply add a with() method that flashes that message to the session for the next request:
return redirect()->route('pages.index')->with('success', 'Hello'. $name.', Your Form was successfully sent');
Or rather if your form was originally from the index page, then you simply don't have to remember the name of the route, simply use back() helper method, i.e:
return back()->with('success', 'Hello'. $name.', Your Form was successfully sent');
I'm having trouble with an error that I'm getting in laravel. When I run my code on localhost I don't have any issues, but when I placed laravel in demo live server which is server ('https') I get the MethodNotAllowedHttpException error.
Here's my code for my route
Route::post('post_reminder', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
});
And here's the form code
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li>
#endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}<br /><br /><br />
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }}
</p>
{{ Form::close() }}
</div>
<div id="reset_container">
{{ Form::open(array('url' => 'post_reminder','method' => 'post')) }}
<h1 id="pass_recovery_text">Password Recovery Form</h1>
<p>
#if (Session::has('error'))
<li id="error">{{ trans(Session::get('reason')) }}</li>
#elseif (Session::has('success'))
<li id="error">An e-mail with the password reset has beensent.</li>
#endif
#foreach($errors->all() as $error)
<li id="error">{{ $error }}</li> #endforeach
{{ Form::label('email', 'Please enter you email: ') }}
{{ Form::text('email','',array('id' => 'forgot')) }}
{{ Form::submit('Reset') }}
{{ HTML::link('/', 'Have a account Sign-In', array('id' => 'sign-in')) }} </p>
{{ Form::close() }}
Have you tried force the route to be served over https?
Route::post('post_reminder', array('https', function(){
$creds = array(
'email' => Input::get('email')
);
$rules = array(
'email' => 'required|email'
);
$messages = array(
'email' => 'The :attribute needs to be an real email'
);
$validator = Validator::make($creds, $rules,$messages);
if($validator->fails())
{
return Redirect::route('getReminder')->withErrors($validator);
}
else
{
return Password::remind($creds);
}
}));
Referencing to http://laravel.com/docs/4.2/routing here.