Laravel MethodNotAllowedHttpException, but my methods match - php

I have a registration form that I'm creating using the blade templating like so:
{{ Form::open(array('url'=>'registerUser', 'method'=>'POST', 'class'=>'loginForm SignUp')) }}
In routes.php, I have the following route:
Route::post('registerUser', 'UsersController#doRegister');
But when I submit the form, I get a MethodNotAllowedHttpException. Pretty much every other question I've found online about this was a case of the form having the GET method while the route had POST, but mine match, so I'm pretty confused.
Edit: Here's my full routes file:
Route::get('', 'UsersController#showLogin');
Route::get('login', 'UsersController#showLogin');
Route::post('doLogin', 'UsersController#doLogin');
Route::get('signUp', 'UsersController#showSignUp');
Route::post('authenticateCode', 'UsersController#authenticateCode');
Route::post('requestCode', 'UsersController#requestCode');
Route::get('showRegister', 'UsersController#showRegister');
Route::post('registerUser', 'UsersController#doRegister');
Route::get('dashboard', 'DashboardController#showDashboard');
Here's the controller function:
public function doRegister() {
$rules = array(
'fname' => 'required|alpha|min:2',
'lname' => 'required|alpha|min:2',
'email' => 'required|email|unique:users',
'phone' => 'required|alpha_num|min:7',
'company' => 'required|alpha_spaces|min:2',
'password' => 'required|alpha_num|between:6,12|confirmed', // these password rules need to be improved
'password_confirmation' => 'required|alpha_num|between:6,12'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->passes()) {
$user = User::create(array(
'fname' => Input::get('fname'),
'lname' => Input::get('lname'),
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password')),
'phone' => Input::get('phone'),
'company' => Input::get('company')
));
// Update the invite key that was passed to the register page
// with this new user's ID.
$key = InviteKey::find(Input::get('key'));
$key->user_id = $user->id;
$key->save();
return Redirect::to('login')->with('message', 'Thank you for registering!');
} else {
return Redirect::to('register')
->with('message', 'The following errors occurred')
->withErrors($validator)
->withInput(Input::except('password'));
}
}

Related

Concaternate Array value with Ternary Operator result

I'm using PHP 7 and Laravel 6. I got errors when I made a user request rule and used it in user controller. The request rule I made is to be reusable in create and update function, so if i pass the id of user, it will validate the unique of user except that id. But if not, it will search all the ids and validate if it's unique. I follow BaM solution, here: https://stackoverflow.com/a/24205849
This my UserRequest.php:
public static function rules ($id=0, $merge=[]) {
return array_merge(
[
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users' . ($id ? ",$id" : ''),
'phone_number' => 'required|string|min:9|max:10|unique:users' . ($id ? ",$id" : ''),
'user_img' => 'required|mimes:jpeg,jpg,png,gif|max:10000',
],
$merge);
}
This is my UserController:
public function store(Request $request)
{
$extend_rules = [
'pass' => 'required|string|min:8',
];
$validator = Validator::make($request->all(), UserRequest::rules($extend_rules));
if ($validator->fails())
{
return redirect()->back();
}
$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->pass),
'phone_number' => $request->phone_number,
'user_img' => $request->user_image->store('user_img'),
]);
$user->save();
Session::flash('message', 'Your account is successfully created !');
Session::flash('alert-class', 'alert alert-success');
return redirect()->route('users.index');
}
And I got this errors:
ErrorException: Array to string conversion
I tried to search for solutions but couldn't seem to find anything much my case.
If anyone know, please help me!
That's because you're just passing an array while the method accept two different type of parameter
$validator = Validator::make($request->all(), UserRequest::rules(0, $extend_rules)); // <-- you need to either pass 1 or 0 for the id and then $extended rules
// here is your method signature
public static function rules ($id=0, $merge=[]) {

Laravel 5 mewebstudio/captcha not working

I have got problem with Laravel 5 mewebstudio/captcha.
I install it and on my page is captcha image
echo captcha_img()
echo Form::text('captcha','',["class"=>"form-control","placeholder"=>trans('page.captcha')]);
This works fine.
But the problem is with the validation, after the validation form I get a message about incorrect captcha.
My validation code:
if (Request::isMethod('post')){
//$data = Input::except(array('_token'));
$data = Input::all();
$rule = array(
'name' => 'required',
'firstname' => 'required',
'bdate' => 'required',
'email' => 'required|email',
'password' => 'required|min:6|same:password_repeat',
'password_repeat' => 'required|min:6',
'captcha' => 'required|captcha'
);
$validator = Validator::make($data, $rule);
if ($validator->fails()) {
$errors = $validator->messages();
}
}
I think captcha session is not working because after dump session I don't have captcha key which should be put in session (I found in Captcha.php )
$this->session->put('captcha', [
'sensitive' => $this->sensitive,
'key' => $this->hasher->make($this->sensitive ? $bag : $this->str->lower($bag))
]);
In Laravel 5.2 you need to change line 29 in CaptchaServiceProvider to
$this->app['router']->group(['middleware' => 'web'], function () {
$this->app['router']->get('captcha/{config?}', '\Mews\Captcha\CaptchaController#getCaptcha');
});
I solved this problem.
In Laravel 5.2 you need to change line 26 in CaptchaServiceProvider to
$this->app['router']->get('captcha/{config?}', \Mews\Captcha\CaptchaController#getCaptcha')->middleware('web');

Authenticating registration against two tables: Laravel 5.1

I'm new to Laravel and am trying to figure out how to authenticate against two tables during new user registration.
I've modified the default methods in AuthController - I'm checking to see if a store number is valid, and if it is, register the user. This works fine - if the store number provided checks out, the user is inserted into both tables (user and user_store) and redirected to the dashboard page.
However, if the validation against $store is false, then I receive the following error
Argument 1 passed to Illuminate\Auth\Guard::login() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Http\RedirectResponse given
As you can see in the code I'm just trying to redirect to the auth/register view and provide an error message that the store number was invalid. Where am I going wrong?
SEE UPDATED CODE BELOW THIS BLOCK...
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
}
protected function create(array $data)
{
if(!$store = Store::where('number', $data['store_number'])->first()) {
// HERE'S WHERE I'M HAVING THE PROBLEM
return redirect('auth/register')->withErrors('store_number','Could not find a match for the Store Number');
} else {
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$data['sid'] = $store->id;
$data['uid'] = $user->id;
$store = UserStore::create($data);
return $user;
}
}
UPDATE
I've since moved this into the validator() method, because, well, it makes more sense to do the validation in the validator() method... right?
Here's my new code.
protected function validator(array $data)
{
if(!Store::where('number', $data['store_number'])->first()) {
// still not working!
return redirect('auth/register')->withErrors('store_number','Could not find a match for the Store Number');
}
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
}
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
$s['sid'] = $data['store_number'];
$s['uid'] = $user->id;
$store = UserStore::create($s);
return $user;
}
And here's my new error message
BadMethodCallException in RedirectResponse.php line 198:
Method [fails] does not exist on Redirect.
Figured it out. I needed to use the After validation hook inside the validator() method. :)
protected function validator(array $data)
{
$validator = Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:user',
'password' => 'required|min:6',
'store_number' => 'required',
]);
$validator->after(function($validator) {
if(!Store::where('number', $_POST['store_number'])->first()) {
$validator->errors()->add('store_number', 'Could not find a match for the Store number');
}
});
return $validator;
}

Laravel 4 Auth::attempt() issue

I'm trying the Laravel's Auth class but the method returns false always. Here's my code:
Controller :
public function postLogin()
{
// Declare the rules for the form validation.
//
$rules = array(
'email' => 'Required|Email',
'password' => 'Required'
);
// Get all the inputs.
//
$email = Input::get('email');
$password = Input::get('password');
// Validate the inputs.
//
$validator = Validator::make(Input::all(), $rules);
// Check if the form validates with success.
//
if ($validator->passes())
{
//echo $password; displays test
// Try to log the user in.
//
if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
// Redirect to the users page.
//
return Redirect::to('account')->with('success', 'You have logged in successfully');
}
else
{
// Redirect to the login page.
//
return Redirect::to('account/login')->with('error', 'Email/password invalid.');
}
}
// Something went wrong.
//
return Redirect::to('account/login')->withErrors($validator->getMessageBag());
}
Seeder.php
public function run()
{
DB::table('users')->delete();
$users = array(
array(
'email' => 'test#test.com',
'password' => Hash::make('test'),
'first_name' => 'John',
'last_name' => 'Doe',
'created_at' => new DateTime,
'updated_at' => new DateTime,
)
);
DB::table('users')->insert( $users );
}
It will be because of framework bug. So try to update it.
composer update
Or
php composer.phar update
In your config/auth.php file
try changing from 'driver' => 'eloquent' to 'driver' => 'database'.

comparing confirmation password against a hashed password | Laravel 4

I am trying to get the confirmation password to work against the password field in my form. I went through the Validator methods and they all seem to work perfectly. However, when trying to confirm the password I get an error message everytime that they must match..scratching my head I can only determine it's because they are being hashed before going through validation. I am not sure how to get past this as they need to be hash before being entered into the database. Any ideas?
getSignUp Controller
public function getSignUp() {
$userdata = array(
'email' => Input::get('email'),
'password' => Hash::make(Input::get('password')),
'confirm_password' => Hash::make(Input::get('confirm_password')),
'user_zip_code' => Input::get('user_zip_code')
);
$rules = array(
'email' => 'required|email|unique:users,email',
'password' => 'required|min:5',
'confirm_password' => 'required|same:password',
'user_zip_code' => 'required'
);
$validation = Validator::make($userdata, $rules);
if($validation->fails()){
return Redirect::to('signup')->withErrors($validation)->withInput();
}
$user = new User($userdata);
$user->save();
return Redirect::to('login');
}
If anymore code is needed let me know. I just simply have the withErrors going to the blade template for the signup page
Don't pass the hashed password to the validator. Hash it before you save it:
public function getSignUp() {
$userdata = array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'confirm_password' => Input::get('confirm_password'),
'user_zip_code' => Input::get('user_zip_code')
);
$rules = ...
$validation = Validator::make($userdata, $rules);
if($validation->fails()){
return Redirect::to('signup')->withErrors($validation)->withInput();
}
$userdata['password'] = Hash::make($userdata['password']);
$user = new User($userdata);
$user->save();
return Redirect::to('login');
}

Categories