Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle User Registration via API Laravel

I have being reading and watching some tutorials about API development in Laravel. I am new to API development entirely although I have used Laravel a bit.

From all the tutorials I have gone through, they handled: login, get some data, update information, delete information, and even insert some information into the database.

My problem is that none of this tutorials handle something like a user registration.

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    Route::resource('users', 'UsersController');
    Route::resource('messages', 'MessagesController');
});

From the code above, it assumes that user must have registered before gaining access to the controllers cause of the auth.basic.

So, my question is this: How can I handle registration? cause it doesn't seem I will group it with the codes above.

like image 878
Diamond Avatar asked Oct 20 '22 07:10

Diamond


1 Answers

You can't place the registration routes in a route group with a auth.basic filter. Then only users that are logged in can register.

So you should make new routes for registration:

// Opens view to register form
Route::get('register', array('as'=>'register', 'uses'=>'UserController@getRegister'));
// Handles registering
Route::post('register', array('uses'=>'UserController@postRegister'));

The URL would become: http://yourhost/register

Or if you still want to use your prefix, you can group:

Route::group(array('prefix'=>'api/v1'), function(){
  // Opens view to register form
  Route::get('register', array('as'=>'register', 'uses'=>'UserController@getRegister'));
  // Handles registration
  Route::post('register', array('uses'=>'UserController@postRegister'));
});

The URL would become: http://yourhost/api/v1/register

Then you create a getRegister() and postRegister() method in your UserController:

<?php

class UserController extends BaseController{

 public function getRegister(){
   // Return register form
   return View::make('users.register');
 }

 public function postRegister(){
   // Validate post info and create users etc. 
 }

There are tons of tutorials to help you with user registration in Laravel,

http://code.tutsplus.com/tutorials/authentication-with-laravel-4--net-35593

like image 74
Maurice Avatar answered Oct 25 '22 06:10

Maurice