I am trying to post a form in laravel an getting this error:
Route [AdminController#postLogin] not defined
// Controller
public function postLogin()
{
$rules = array(
'email' => 'required|email',
'password' => 'required'
);
$validation = Validator::make(Input::all(),$rules);
if($validation->fails())
{
return View::make('admin.login')->withErrors($validation);
}
else
{
}
}
// View
<div id="wrapper">
<div id="subwrapper">
#if($errors->has())
#foreach($errors as $error)
<p>{{ $error }}</p>
#endforeach
#endif
{{ Form::open(array('action' => 'AdminController#postLogin')) }}
<input type="email" name="email" placeholder="Email" required/><br />
<input type="password" name="password" placeholder="Password" required/><br />
<input type="submit" name="submit" value="Login" />
{{ Form::close() }}
</div>
</div>
I have also tried adding seperate route and then routing the form to that using
Route::get('/adminshashishekhar', array('as' => 'admin', 'uses' => 'AdminController#index'));
Route::post('/postlogin', array('as' => 'postlogin', 'uses' => 'AdminController#postLogin'));
but then I get MethodNotAllowedHttpException
I think it's something with the form.
Form::open(array('action' => 'AdminController#postLogin'))
I know Laravel allows the 'action', but I've never used it. I've only ever done:
Form::open(array('url' => 'postlogin'))
Now I have no idea if that will fix it, but could be worth a shot.
Related
I have tried everything and I can't figure out where comes my mistake.
the update() method doesn't update anaything, i only get back "No message" error...
Here's Routes in web.php:
Route::get('/user/edit/{id}', ['as' => 'users.edit', 'uses' => 'UserAdController#edit']);
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
the view users/edit.blade.php :
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', $ad->id)}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('post') }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
</div>
#endsection
Update method from UserAdController:
public function update($id, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
$data = \App\Ad::find($id);
$data->title = $request->get('title');
$data->price = $request->get('price');
$data->content = $request->get('content');
$data->save();
return redirect()->back()->with('success', 'Data updated');
}
I'm not a laravel dev. I just stumbled on the documentation. You should also add the csrf field to your blade
In edit.blade.php, add this after the opening <form> tag
{{csrf_field()}}
Also the parameters in your update method aren't well arranged
It should be
public function update(Request $request, $id) {
}
The second parameter($id), comes from what you've defined as your routes in web.php file
Route::post('/user/update/{id}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
Where {id} would be replaced by the original id
Try this instead
public function update(Request $request){
//your code here
}
Request->only() returns an array with one element, While validator is the most common way to handle validation for the incoming request.
use Validator;
public function update(Request $request, $id){
$v = validator($request->only('title', 'price', 'content'), [
'title' => 'required|string|max:255',
'price' => 'required|integer',
'content' => 'required',
]);
$data = request()->only('title','price','content');
$userData = ([
'title' => $data['title'],
'price' => $data['price'],
'content' => $data['content'],
]);
$data = \App\Ad::find($id);
$data->update($userData);
return response()->json($data);
}
Thank you all !!
Seems like I wasn't doing it right.
I needed to add {{csrf_field()}} in edit form and use $request->only()
I think it would be better if you use put method like this:
Route::put('ad/{ad}', ['as' => 'users.update', 'uses' =>'UserAdController#update']);
update your form to be like this:
<div class="container">
<br>
<h3>Edit your ad</h3>
<br>
<form method="post" action="{{route('users.update', ['ad' => $ad->id])}}">
<input name="_method" type="hidden" value="PATCH">
{{ method_field('put') }}
{{ csrf_field() }}
<div class="form-group">
<label for="title">Title</label>
<input type="text" name="title" class="form-control" id="title" value="{{$ad->title}}">
</div>
<div class="form-group">
<label for="title">Price</label>
<input type="text" name="price" class="form-control" id="title" value="{{$ad->price}}">
</div>
<div class="form-group">
<label for="content">Your content</label>
<textarea name="content" class="form-control" id="content" rows="3">{{$ad->content}}</textarea>
</div>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-info">
</div>
</form>
and now your update function :
public function update(\App\Ad $ad, Request $request){
$request->validate([
'title'=>'required',
'price'=> 'required|integer',
'content' => 'required'
]);
//$data = \App\Ad::find($id);
$ad->update([
"title" => $request->title,
"price" => $request->price,
"content" => $request->content,
]);
return redirect()->back()->with('success', 'Data updated');
}
when you get use to put, delete and patch methods you can read about Route::resource and your code will be easier.
Here the signin function is working fine but $this->validate() function returns nothing in view page. tried various method suggested in other Q&As. seems something wrong with helpers.
userController
public function postSignIn(Request $request)
{
$this->validate($request, [
'email' => 'bail|required',
'password' => 'required'
]);
if (Auth::attempt([ 'email' => $request['email'], 'password' => $request['password'] ])){
return redirect()->route('dashboard');
}
return redirect()->back();
}
Login Page
#if ($errors->any())
<div>
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
<form name="loginForm" action="{{ route('signin') }}" method="post" autocomplete="disable">
<div class="form-group mb-4">
<input type="email" class="form-control {{ $errors->has('email')? 'is-invalid' : ''}}" name="email" aria-describedby="emailHelp" placeholder=" " />
<label for="loginFormInputEmail">Email address</label>
</div>
<div class="form-group mb-4">
<input type="password" class="form-control {{ $errors->has('password')? 'is-invalid' : ''}}" name="password" placeholder="Password" />
<label for="loginFormInputPassword">Password</label>
</div>
Probably another issue would be with $errors not being saved to Session variable $errors and nothing being shown in the view.
Here is an example of the same issue: http://laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation
For me the solution defined in the above link worked. Solution: Is in app\Http\Kernel.php Move \Illuminate\Session\Middleware\StartSession::class, from $middlewareGroups to $middleware
Before
After
Source Thrade
: Laravel MessageBag errors array is empty in view but with content if I kill script
I'm trying to make login form in Laravel 4.2 + Sentry . The problem is that when I submit the form I got the error that method is not allowed.
When I check my form in source it has method="POST" and also in the route I've wrote post. What can be the problem?
MethodNotAllowedHttpException
but can't see why? This is the form
{{ Form::open(array('route' => 'check-auth')) }}
<div class="body bg-gray">
{{-- Display flash message --}}
#if (Session::has('flash_message'))
<span style="margin-left:18%; color: #ff0000">{{ Session::get('flash_message') }}</span>
#endif
<div class="form-group">
<input type="text" name="email" class="form-control" placeholder="User email"/>
#if($errors->has('login_errors')) <span class="has-error">{{ $errors->first('email') }}</span> #endif
</div>
<div class="form-group">
<input type="password" name="password" class="form-control" placeholder="User password"/>
</div>
<button type="submit" name="submitbtn" class="btn bg-olive btn-block">Sign me in</button>
</div>
{{ Form::close() }}
Route
Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController#login']);
and controller
public function login()
{
try{
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$user = Sentry::authenticate($credentials, false);
if($user){
return Redirect::to('dashboard');
}
return Redirect::to('/')->with('title','Login errors');
}
catch(Exception $e){
echo $e->getMessage();
Session::flash('flash_message', 'No access!');
return Redirect::to('/')->with('title','Login errors');
}
}
UPDATE: Error
production.ERROR: Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException in /var/www/html/time/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:210
You're route is correct, the only thing I could suggest would be to append the type to the opening of the form:
{{ Form::open(['url' => 'check-auth', 'method' => 'post']) }}
{{ Form::open(array('route' => 'check-auth')) }}
See, You are using route check-auth and in routes file you defined a different route i.e user-login
Route::post('user-login', ['as'=>'check-auth', 'uses'=>'AuthenticationController#login']);
Correct route and try again this will work
Instead of post use get (it can still post things but your are able to retrieve too)
My Laravel blog project the get method of postcontroller is working but post method is not working. I redirect The Post Method. But Accordind to my my code it should return to my Admin page.My Controller Code is
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;
use App\Category;
class PostController extends Controller
{
public function getBlogIndex() {
return view('frontend.blog.index');
}
public function getSinglePost($post_id,$end='frontend') {
return view($end . '.blog.single');
}
public function getCreatePost() {
return view('admin.blog.create_post');
}
public function postCreatePost(Request $request ) {
$this->validate($request, [
'title' => 'required|max:120|unique:posts',
'author' => 'required|max:80',
'body' => 'required'
]);
$post = new Post();
$post->title = $request['title'];
$post->author = $request['author'];
$post->body = $request['body'];
$post->save();
return redirect()->route('admin.index')->with(['success' => 'Post Successfully Created']);
}
}
My Routes file
<?php
Route::group(['middleware' => ['web']], function () {
Route::group([
'prefix' =>'/admin'
], function() {
Route::get('/', [
'uses' => 'AdminController#getIndex',
'as' => 'admin.index'
]);
Route::get('/blog/posts/create', [
'uses' => 'PostController#getCreatePost',
'as' => 'admin.blog.create_post'
]);
Route::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
});
});
My form is
#extends('layouts.admin-master')
#section('styles')
{!! Html::style('src/css/form.css') !!}
#endsection
#section('content')
<div class="container">
#include('includes.info-box')
<form action="{{ route('admin.blog.post.create') }}" method="post">
<div class="input-group">
<label for="title">Title</label>
<input type="text" name="title" id="title" {{ $errors->has('title') ? 'class=has-error' : '' }} value="{{ Request::old('title') }}">
</div>
<div class="input-group">
<label for="author">Author</label>
<input type="text" name="author" id="author" {{ $errors->has('author') ? 'class=has-error' : '' }} value="{{ Request::old('author') }}">
</div>
<div class="input-group">
<label for="category_select">Add Category</label>
<select name="category_select" id="category_select">
<option value="Dummy Category ID">Dummy Category</option>
</select>
<button type="button" class="btn">Add Category</button>
<div class="added-categories">
<ul></ul>
</div>
<input type="hidden" name="categories" id="categories">
</div>
<div class="input-group">
<label for="body">Body</label>
<textarea name="body" id="body" rows="12" {{ $errors->has('body') ? 'class=has-error' : '' }} >{{ Request::old('body') }}</textarea>
</div>
<button type="submit" class="btn">Create Post</button>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
</div>
#endsection
#section('scripts')
{!! Html::script('src/js/posts.js') !!}
#endsection
When I submit My post I shows this
I dont find where the problem is. Plz Help Me
This is an extremely common error and a good one to look out for. Whenever you see:
MethodNotAllowedHttpException in RouteCollection.php
The very first thing you should check is your routes file to make sure you have Route::get or Route::post correctly based on what you are trying to do.
Your issue is that your form sends the data as POST, but your route is GET.
<form action="{{ route('admin.blog.post.create') }}" method="post">
and
Route::get('/blog/post/create', [
'uses' => 'PostController#postCreatePost',
'as' => 'admin.blog.post.create'
]);
Change that to Route::post for it to function correctly.
I wrote a laravel form, its screen shot is as given below:
And I use neo4j for storing that form data.
Here is the code:
app/views/duck-form.blade.php
<!doctype html>
<html>
<head>
<title>Laravel Form Validation!</title>
<!-- load bootstrap -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<style>
body { padding-bottom:40px; padding-top:40px; }
</style>
</head>
<body class="container">
<div class="row">
<div class="col-sm-8 col-sm-offset-2">
<div class="page-header">
<h1><span class="glyphicon glyphicon-flash"></span> Register! </h1>
</div>
#if ($errors->has())
<div class="alert alert-danger">
#foreach ($errors->all() as $error)
{{ $error }}<br>
#endforeach
</div>
#endif
<!-- FORM STARTS HERE -->
<form method="POST" action="/ducks" novalidate>
<div class="form-group #if ($errors->has('name')) has-error #endif">
<label for="name">Name</label>
<input type="text" id="name" class="form-control" name="name" placeholder="Enter your name" value="{{ Input::old('name') }}">
#if ($errors->has('name')) <p class="help-block">{{ $errors->first('name') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('email')) has-error #endif">
<label for="email">Email</label>
<input type="text" id="email" class="form-control" name="email" placeholder="Enter your email id" value="{{ Input::old('email') }}">
#if ($errors->has('email')) <p class="help-block">{{ $errors->first('email') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password')) has-error #endif">
<label for="password">Password</label>
<input type="password" id="password" class="form-control" name="password">
#if ($errors->has('password')) <p class="help-block">{{ $errors->first('password') }}</p> #endif
</div>
<div class="form-group #if ($errors->has('password_confirm')) has-error #endif">
<label for="password_confirm">Confirm Password</label>
<input type="password" id="password_confirm" class="form-control" name="password_confirm">
#if ($errors->has('password_confirm')) <p class="help-block">{{ $errors->first('password_confirm') }}</p> #endif
</div>
<button type="submit" class="btn btn-success">Submit</button>
</form>
</div>
</div>
</body>
</html>
1. I added 'Artdarek\Neo4j\Neo4jServiceProvider' to providers array in app/config/app.php.
2. I added neo4j configuration in app/config/database.php
'neo4j' => [
'default' => [
'host' => 'localhost',
'port' => 7474,
'username' => null,
'password' => null,
],
],
3. Then I added a controller for that form:
<?php
class DuckController extends BaseController {
public function showWelcome()
{
return View::make('duck');
}
}
4. This is my routes.php.
<?php
Route::get('/', function()
{
return View::make('hello');
});
// route to show the duck form
Route::get('ducks', function()
{
return View::make('duck-form');
});
// route to process the ducks form
Route::post('ducks', array('before' => 'csrf', function()
{
// create the validation rules ------------------------
$rules = array(
'name' => 'required', // just a normal required validation
'email' => 'required|email|unique:ducks', // required and must be unique in the ducks table
'password' => 'required',
'password_confirm' => 'required|same:password' // required and has to match the password field
);
// create custom validation messages ------------------
$messages = array(
'required' => 'The :attribute is really really really important.',
'same' => 'The :others must match.'
);
// do the validation ----------------------------------
// validate against the inputs from our form
$validator = Validator::make(Input::all(), $rules, $messages);
// check if the validator failed -----------------------
if ($validator->fails()) {
// redirect our user back with error messages
$messages = $validator->messages();
// also redirect them back with old inputs so they dont have to fill out the form again
// but we wont redirect them with the password they entered
return Redirect::to('ducks')
->withErrors($validator)
->withInput(Input::except('password', 'password_confirm'));
} else {
// validation successful ---------------------------
// our duck has passed all tests!
// let him enter the database
// create the data for our duck
$duck = new Duck;
$duck->name = Input::get('name');
$duck->email = Input::get('email');
$duck->password = Hash::make(Input::get('password'));
// save our duck
$duck->save();
// redirect ----------------------------------------
// redirect our user back to the form so they can do it all over again
return Redirect::to('ducks')
->with('messages', 'Hooray!');
}
}));
5. This is my model file for form:
<?php
class Duck extends Eloquent {
protected $fillable = array('name', 'email', 'password');
}
6. This is my model for neo4j:
<?php
//use Illuminate\Auth\EloquentUserProvider;
class database extends Eloquent {
public function index($name, $email, $password, $password_confirm) {
$formData = Neo4j::makeNode();
$formData->setProperty('name',$name)
->setProperty('email',$email)
->setProperty('password',$password)
->setProprty('password_confirm',$password_confirm)
->save();
}
}
When I click on that submit button in that form, I get this error:
[ Edit ]
I was working on it:
This is the new error which I got:
Is it a csrf token issue?
It is pointing to:
Route::filter('csrf', function()
{
if (Session::token() != Input::get('_token'))
{
throw new Illuminate\Session\TokenMismatchException;
}
});
I'm unable to resolve it since 5 hours and data isn't getting stored in neo4j DB. How can I fix it?
When using the CSRF Protection Filter, your form must be declared in your blade as such:
{{ Form::open(array('method' => 'POST', 'action' => URL::to("/ducks"))) }}
And closed:
{{ Form::close() }}
This will render the same form in your html as using:
<form method="POST" action="{{ URL::to("/ducks") }}">...</form>
But will also add the hidden _token element that you are missing:
<input type="hidden" name="_token" value="value_of_token"/>
Hope that helps!
Edit
Or, if you don't want to recreate your <form>, you may simply use:
{{ Form::token() }}
Somewhere inside your existing <form> tag to create it.
Your code looks fine, might be you need to run
composer dump-autoload