Both routes use the same controller method and likely return the same view. That they act exactly the same is expected. If you want them to be different, send the get requests to different controller methods and return different views with different forms.
Whats wrong with my routes.php file?
Hi guys, new here, and also new to Laravel, and must say im loving it so far!
I have a site im working on, that has 2 registration forms, for 2 different users. Its basically the same form just with a hidden value (either 0 or 1) that is different, along with the overall look of the page. Mechanically its exactly the same as the other form.
I have setup my routes.php file as such :
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::get('auth/performer-registration', 'Auth\AuthController@getRegister');
Route::post('auth/performer-registration', 'Auth\AuthController@postRegister');
I have 2 files within resources\views\auth :
register.blade.php
performer-registration.blade.php
When i goto /auth/register i get the registration form, when i goto /auth/performer-registration i get the same form....
Its like register and performer-registration both show the same page (~in fact they do!). Im not sure where im going wrong!
Any suggestions would be great.
Thanks in advance, Eddy
please edit your question and put three back ticks ` before and after.
From what I can see, both routes call the getRegister method. How did you expect to serve up the performer form?
create a getPerformerRegister method and then call that.
Thanks for the responses guys, really appreciated!
Im really new to this, so go easy...
With creating a new method ie. getPerformerRegister, where would i add that? Into AuthController?
Thanks again!
Yes. You would make a getPerformerRegister method in the AuthController and then change the line in routes to look like this:
Route::get('auth/performer-registration', 'Auth\AuthController@getPerformerRegister');
You would maybe have to change the post route too to make sure the additional field is saved, but Im not sure. Try it to see if it works first.
Yep.
Hmm might sound stupid, and new but how do i create a new method?
Go to the AuthController and make one.
public function getPerformerRegister()
{
return view('my.view');
}
public function getPerformerRegister (array $data)
{
$this->redirectTo = '/auth/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$secretpass = str_random(8);
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'secretpass' => $secretpass,
'password' => bcrypt($data['password']),
]);
}
Returns this error :
ReflectionException in Route.php line 280:
Method App\Http\Controllers\Auth\AuthController::getPerformerRegister () does not exist
PS > Thanks for the help!
Can you paste your routes file again?
<?php
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('home');
});
Route::get('/auth/welcome', function () {
return view('auth/welcome');
});
Route::get('/dashboard', function () {
return view('dashboard/user');
});
Route::get('/about', function () {
return view('information/about');
});
Route::get('/terms', function () {
return view('information/terms');
});
Route::get('/privacy', function () {
return view('information/privacy');
});
Route::get('/cookies', function () {
return view('information/cookies');
});
Route::get('/contact', function () {
return view('information/contact');
});
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@logout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
Route::get('auth/performer-registration', 'Auth\AuthController@getPerformerRegister ');
Route::post('auth/performer-registration', 'Auth\AuthController@postPerformerRegister ');
Route::controllers([
'password' => 'Auth\PasswordController',
]);
You have a space after getPerformerRegister and postPerformerRegister in the routes file.
Youre probably going to run into another error as well. You are type hinting array in your controller method but you will be receiving a Request object. That will probably throw an error.
ErrorException in AuthController.php line 89:
Argument 1 passed to App\Http\Controllers\Auth\AuthController::getPerformerRegister() must be of the type array, none given
Yep...unfortunately that means nothing to me! Where do i start?
Thanks again for your help!
Yeah, you are type hinting array as a required parameter but there is nothing being sent.
public function getPerformerRegister ()
{
}
This method should only be getting a view for the user, not registering the user. Do that in the postPerformerRegister method.
Ok i think i understand the problem...
So i done this :
public function getPerformerRegister()
{
return view('auth.performer-registration');
}
public function postPerformerRegister(array $data)
{
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$secretpass = str_random(8);
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'secretpass' => $secretpass,
'password' => bcrypt($data['password']),
]);
}
And now i get this error :
Argument 1 passed to App\Http\Controllers\Auth\AuthController::postPerformerRegister() must be of the type array, none given
But arent i creating an array here...?
public function postPerformerRegister(array $data)
No, in the get method, you are type hinting an array and demanding an array, but nothing is being passed to this method. The get method should only be returning a view.
What is type hinting? :-\
Im so confused...lol
Isnt this just getting a view though?
public function getPerformerRegister()
{
return view('auth.performer-registration');
}
Yes. That is what you want to do. You are getting the view that contains the form.
Type hinting is where you declare the type of the parameters passed to a method. It means that the parameters must be of the declared type.
public function postPerformerRegister(array $data)
{
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$secretpass = str_random(8);
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'secretpass' => $secretpass,
'password' => bcrypt($data['password']),
]);
}
Isnt this then, just reusing the array previously created from here :
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'username' => 'required|unique:users',
'email' => 'required|email|max:255|unique:users',
'role' => 'required|max:255',
'password' => 'required|min:6|confirmed',
]);
}
The 'array' (its actually a request object) is created when the user fills in the form on the website. The validate method doesn't create amything, it just validates the user supplied input.
So where in here is the data stored to be used?
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'username' => 'required|unique:users',
'email' => 'required|email|max:255|unique:users',
'role' => 'required|max:255',
'password' => 'required|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$secretpass = str_random(8);
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'secretpass' => $secretpass,
'password' => bcrypt($data['password']),
]);
}
Thats what was originally in my authController
Im just confused as to where/how the data comes from the form, to the database...
The methods that handle that are in the trait AuthenticatesAndRegistersUsers. Its in vendor at Illuminate\Foundation\Auth or you can see it on github here: https://github.com/laravel/framework/blob/5.0/src/Illuminate/Foundation/Auth/AuthenticatesAndRegistersUsers.php
Im just genuinely stumped as to what im meant to be doing here....lol
Am i meant to edit the trait file?
No. The AuthController uses the trait. You shouldn't edit the trait, just put your methods in the AuthController.
I still dont understand.
protected function create(array $data)
{
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$secretpass = str_random(8);
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'secretpass' => $secretpass,
'password' => bcrypt($data['password']),
]);
}
Works fine, but...
public function postPerformerRegister(array $data)
{
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'password' => bcrypt($data['password']),
]);
}
Doesnt work? Although its basically a direct copy?
OK! Progress...
So i've managed to get the form to work by doing this :
public function postPerformerRegister(Request $request)
{
$data = $request->all();
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data)
{
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
return User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'password' => bcrypt($data['password']),
]);
}
This posts the data to the database, and allows the user to login. but rather than returning to /welcome it actually just shows the posted information on a blank page!
Thats because you are returning the User object. Try this:
public function postPerformerRegister(Request $request)
{
$data = $request->all();
$this->redirectTo = '/welcome';
Mail::send('email.password', $data, function($message) use ($data) {
$message->to($data['email'], $data['name'])->subject('Welcome to Models Allowed');
});
$user = User::create([
'name' => $data['name'],
'username' => $data['username'],
'email' => $data['email'],
'role' => $data['role'],
'password' => bcrypt($data['password']),
]);
return redirect($this->redirectPath());
}
Perfect! :D
You're a star for putting up with me, thankyou so much for your help!
No problem :) Glad you got it working.
Please or to participate in this conversation.