PHP Tutorial Laravel 4 Framework (Authentication) - Creating User Account II
Laravel 4
- Installing on Ubuntu - local
- Installing on a Shared host
- Installing on Windows
- Creating users table
- Home page with controller and blade
- Blade Templating
- Database connection and sending emails
- Creating user account I - GET
- Creating user account II - POST
- Creating user account III - Error checking & redirecting input
- Creating user account IV - User::create()
- Activating user account I - Mail::send()
- Activating user account II - Update user's status
- User account sign-in I - Route and link
- User account sign-in II - Validation
- User account sign-in III - Login Authentication
- Singing out
- Remember the user
- Changing Password I
- Changing Password II
- Recovering forgotten password I
- Recovering forgotten password II
- User Profile
- Database Migration using artisan
This tutorial is the continuation from Creating user account I.
We can determining if a user is authenticated by using Auth::check():
if (Auth::check()) { // The user is logged in... }
Let's open navigation.blade.php, and put lines of code for checking if the use is logged in or not:
<nav> <ul> <li> <a href="{{ URL::route('home') }}">Home sweet home!! Naviation</a></li> @if(Auth::check()) @else <li><a href="{{ URL::route('account-create') }}">Create an account</a></li> @endif </ul> </nav>
If we hit the root:
When we click the link for 'Create an account', we get:
We may want to use template (create.blade.php):
@extends('layout.main') @section('content') Create an account by template @stop
Then, if we hit the 'Create an account' link from the root page, we get:
Now, it's time for post. We want to get a form when we click the link for 'Create an account':
- routes.php:
<?php Route::get('/', array('as' => 'home', 'uses' => 'HomeController@home' )); /* Unauthenticated group */ Route::group(array('before' => 'guest'), function() { /* CSRF protection */ Route::group(array('before' => 'csrf'), function() { /* Create an account (POST) */ Route::post('/account/create', array('as' => 'account-create-post', 'uses' => 'AccountController@postCreate' )); }); /* Create an account (GET) */ Route::get('/account/create', array('as' => 'account-create', 'uses' => 'AccountController@getCreate' )); });
- AccountController.php:
<?php class AccountController extends BaseController { /* Viewing the form */ public function getCreate() { return View::make('account.create'); } /* Submitting the form */ public function postCreate() { return "from AccountController.postCreate()"; } }
- create.blade.php:
@extends('layout.main') @section('content') <form action="{{ URL::route('account-create-post') }}" method="post"> <input type="submit" value="Create account"> {{ Form::token() }} </form> @stop
When we hit the 'Create an account', we get:
Note that the token has been generated for us by the form helper.
We get a page for creating an account when we click the button:
Laravel 4
- Installing on Ubuntu - local
- Installing on a Shared host
- Installing on Windows
- Creating users table
- Home page with controller and blade
- Blade Templating
- Database connection and sending emails
- Creating user account I - GET
- Creating user account II - POST
- Creating user account III - Error checking & redirecting input
- Creating user account IV - User::create()
- Activating user account I - Mail::send()
- Activating user account II - Update user's status
- User account sign-in I - Route and link
- User account sign-in II - Validation
- User account sign-in III - Login Authentication
- Singing out
- Remember the user
- Changing Password I
- Changing Password II
- Recovering forgotten password I
- Recovering forgotten password II
- User Profile
- Database Migration using artisan
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization