21.11.2014 Views

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

Laravel Starter - PHP User Group (Myanmar)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Laravel</strong> <strong>Starter</strong><br />

Finally, add the following line to your routes.php file:<br />

Route::get('logout', 'auth@logout');<br />

That's all! Now, when we go to http://myfirst.dev/logout, we'll be logged out and<br />

redirected to our site's index page.<br />

So, how can we find out if someone is logged in? The Eloquent and Fluent Auth drivers contain<br />

two methods for handling this, check() and guest(). Let's look at each of these in turn:<br />

ÊÊ<br />

ÊÊ<br />

Auth::check() returns true if a user is currently logged in, and false otherwise.<br />

Auth::guest() is the opposite of Auth::check(). It returns false if a user is<br />

logged in, and true otherwise.<br />

Once you've made sure that a user is logged in, you can use Auth::user() to return the user<br />

record. If you're using the Fluent driver, Auth::user() will return a dumb object containing<br />

the appropriate values from the users table. If you're using the Eloquent driver, Auth::user()<br />

will return an instance of your <strong>User</strong> model. This is very powerful. Let's look at an example:<br />

@if(Auth::check())<br />

You're logged in as {{ Auth::user()->real_name<br />

}}<br />

@endif<br />

As you've seen, <strong>Laravel</strong>'s authentication system is driver-based. In this example, we used the<br />

Eloquent driver. However, you also have the ability to create custom authentication drivers. This<br />

gives you the power to authenticate users with different means and return different types of<br />

data with the standard Auth class' API. Covering the development of custom drivers is outside<br />

the scope of this document. However, it's simple and powerful. Be sure to look into the <strong>Laravel</strong><br />

documentation for more information.<br />

3 – Filters<br />

Now that we have a user administration site with authentication, we need to restrict some pages<br />

on our site to users who have successfully authenticated. We'll do that by using filters.<br />

Filters are functions that can be run before or after routed code. A filter that runs before the<br />

routed code is called a before filter. Similarly well-named is the after filter, which runs after<br />

routed code.<br />

Filters are often used for enforcing authentication. We can create a filter that detects if a user<br />

is not logged in, then redirect him/her to the login form. Actually, we don't need to make this<br />

filter at all. <strong>Laravel</strong> ships with this filter already written. You can find it in your application/<br />

routes.php.<br />

38

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!