Skip to content

Commit 1802324

Browse files
committed
first
0 parents  commit 1802324

30 files changed

+4047
-0
lines changed

authentication.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Authentication
2+
3+
- [Introduction](#introduction)
4+
- [Configuration](#configuration)
5+
- [Basic Usage](#basic-usage)
6+
7+
<a name="introduction"></a>
8+
## Introduction
9+
10+
Lumen is primarily designed for building fast micro-services and APIs; however, if you wish, you may use Laravel's authentication system to authenticate users of your Lumen application.
11+
12+
<a name="configuration"></a>
13+
## Configuration
14+
15+
> **Note:** Using the authentication system will require enabling sessions. You can do so by uncommenting the middleware listed in the default call to `$app->middleware` in your `bootstrap/app.php` file.
16+
17+
The authentication system has several configuration options you can set in your `.env` file:
18+
19+
- `AUTH_DRIVER`
20+
- `AUTH_MODEL`
21+
- `AUTH_TABLE`
22+
23+
The `AUTH_DRIVER` value specifies the authentication driver used by the framework. If `eloquent` is specified as the driver, the Eloquent ORM driver will be utilized, while `database` will specify that the plain "database" driver should be used.
24+
25+
The `AUTH_MODEL` option specifies the name of the Eloquent model to be used for authentication. This model must implement the `Illuminate\Contracts\Auth\Authenticatable` contract. For an example model, check out the `App\User` model included in the full-stack Laravel framework.
26+
27+
The `AUTH_TABLE` option specifies which databse table contains the "users" of your application. Of course, this option only applies when using the `database` authentication driver.
28+
29+
<a name="basic-usage"></a>
30+
## Basic Usage
31+
32+
Unlike Laravel, Lumen does not include any scaffolding for authentication, so you will need to use the authentication libraries manually.
33+
34+
> **Note:** If you intend to use the `Auth` facade, be sure to uncomment the `$app->withFacades()` call in your `bootstrap/app.php` file.
35+
36+
First, let's check out the `attempt` method:
37+
38+
use Illuminate\Http\Request;
39+
40+
$app->post('auth/login', function(Request $request) {
41+
42+
if (Auth::attempt($request->only('email', 'password'))) {
43+
return redirect('dashboard');
44+
}
45+
46+
});
47+
48+
The `attempt` method accepts an array of key / value pairs as its first argument. The `password` value will be [hashed](/docs/hashing). The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the `email` column. If the user is found, the hashed password stored in the database will be compared with the hashed `password` value passed to the method via the array. If the two hashed passwords match, a new authenticated session will be started for the user.
49+
50+
The `attempt` method will return `true` if authentication was successful. Otherwise, `false` will be returned.
51+
52+
> **Note:** In this example, `email` is not a required option, it is merely used as an example. You should use whatever column name corresponds to a "username" in your database.
53+
54+
#### Authenticating A User With Conditions
55+
56+
You also may add extra conditions to the authentication query:
57+
58+
if (Auth::attempt(['email' => $email, 'password' => $password, 'active' => 1])) {
59+
// The user is active, not suspended, and exists.
60+
}
61+
62+
#### Determining If A User Is Authenticated
63+
64+
To determine if the user is already logged into your application, you may use the `check` method:
65+
66+
if (Auth::check()) {
67+
// The user is logged in...
68+
}
69+
70+
#### Authenticating A User And "Remembering" Them
71+
72+
If you would like to provide "remember me" functionality in your application, you may pass a boolean value as the second argument to the `attempt` method, which will keep the user authenticated indefinitely, or until they manually logout. Of course, your `users` table must include the string `remember_token` column, which will be used to store the "remember me" token.
73+
74+
if (Auth::attempt(['email' => $email, 'password' => $password], $remember)) {
75+
// The user is being remembered...
76+
}
77+
78+
If you are "remembering" users, you may use the `viaRemember` method to determine if the user was authenticated using the "remember me" cookie:
79+
80+
if (Auth::viaRemember()) {
81+
//
82+
}
83+
84+
#### Authenticating Users By ID
85+
86+
To log a user into the application by their ID, use the `loginUsingId` method:
87+
88+
Auth::loginUsingId(1);
89+
90+
#### Validating User Credentials Without Login
91+
92+
The `validate` method allows you to validate a user's credentials without actually logging them into the application:
93+
94+
if (Auth::validate($credentials)) {
95+
//
96+
}
97+
98+
#### Logging A User In For A Single Request
99+
100+
You may also use the `once` method to log a user into the application for a single request. No sessions or cookies will be utilized:
101+
102+
if (Auth::once($credentials)) {
103+
//
104+
}
105+
106+
#### Manually Logging In A User
107+
108+
If you need to log an existing user instance into your application, you may call the `login` method with the user instance:
109+
110+
Auth::login($user);
111+
112+
This is equivalent to logging in a user via credentials using the `attempt` method.
113+
114+
#### Logging A User Out Of The Application
115+
116+
Auth::logout();
117+

cache.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Cache
2+
3+
- [Configuration](#configuration)
4+
- [Basic Usage](#basic-usage)
5+
6+
<a name="configuration"></a>
7+
## Configuration
8+
9+
The `CACHE_DRIVER` option in your `.env` file determines the cache "driver" to be used for the application. Of course, Lumen supports the same drivers as the full-stack Laravel framework, including Memcached and Redis:
10+
11+
- `array`
12+
- `file`
13+
- `memcached`
14+
- `redis`
15+
- `database`
16+
17+
### Memcached
18+
19+
If you are using the Memcached driver, you may also set the `MEMCACHED_HOST` and `MEMCACHED_PORT` options in your `.env` configuration file.
20+
21+
### Redis
22+
23+
Before using a Redis cache with Laravel, you will need to install the `predis/predis` package (~1.0) via Composer.
24+
25+
### Database
26+
27+
When using the database cache driver, you will need to setup a table to contain the cache items. You'll find an example Schema declaration for the table below:
28+
29+
Schema::create('cache', function($table) {
30+
$table->string('key')->unique();
31+
$table->text('value');
32+
$table->integer('expiration');
33+
});
34+
35+
<a name="basic-usage"></a>
36+
## Basic Usage
37+
38+
> **Note:** If you intend to use the `Cache` facade, be sure to uncomment the `$app->withFacades()` call in your `bootstrap/app.php` file.
39+
40+
#### Storing An Item In The Cache
41+
42+
Cache::put('key', 'value', $minutes);
43+
44+
#### Using Carbon Objects To Set Expire Time
45+
46+
$expiresAt = Carbon::now()->addMinutes(10);
47+
48+
Cache::put('key', 'value', $expiresAt);
49+
50+
#### Storing An Item In The Cache If It Doesn't Exist
51+
52+
Cache::add('key', 'value', $minutes);
53+
54+
The `add` method will return `true` if the item is actually **added** to the cache. Otherwise, the method will return `false`.
55+
56+
#### Checking For Existence In Cache
57+
58+
if (Cache::has('key')) {
59+
//
60+
}
61+
62+
#### Retrieving An Item From The Cache
63+
64+
$value = Cache::get('key');
65+
66+
#### Retrieving An Item Or Returning A Default Value
67+
68+
$value = Cache::get('key', 'default');
69+
70+
$value = Cache::get('key', function() { return 'default'; });
71+
72+
#### Storing An Item In The Cache Permanently
73+
74+
Cache::forever('key', 'value');
75+
76+
Sometimes you may wish to retrieve an item from the cache, but also store a default value if the requested item doesn't exist. You may do this using the `Cache::remember` method:
77+
78+
$value = Cache::remember('users', $minutes, function() {
79+
return DB::table('users')->get();
80+
});
81+
82+
You may also combine the `remember` and `forever` methods:
83+
84+
$value = Cache::rememberForever('users', function() {
85+
return DB::table('users')->get();
86+
});
87+
88+
Note that all items stored in the cache are serialized, so you are free to store any type of data.
89+
90+
#### Pulling An Item From The Cache
91+
92+
If you need to retrieve an item from the cache and then delete it, you may use the `pull` method:
93+
94+
$value = Cache::pull('key');
95+
96+
#### Removing An Item From The Cache
97+
98+
Cache::forget('key');

configuration.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Configuration
2+
3+
- [Introduction](#introduction)
4+
- [After Installation](#after-installation)
5+
- [Configuration Files](#configuration-files)
6+
- [Pretty URLs](#pretty-urls)
7+
8+
<a name="introduction"></a>
9+
## Introduction
10+
11+
Unlike Laravel, Lumen only uses a single `.env` configuration file which can be used to configure the various aspects of the framework. The `.env.example` that ships with the framework can be used as a starting-point for your Lumen configuration.
12+
13+
If you would like to use the `vlucas/phpdotenv` library to load your environment variables into the `$_ENV` PHP super-global, you should uncomment the call to `Dotenv::load` in your `bootstrap/app.php` file.
14+
15+
<a name="after-installation"></a>
16+
## After Installation
17+
18+
Lumen needs very little configuration out of the box. However, you should set your `APP_KEY` configuration option in the `.env` file. This value should be a 32 character, random string.
19+
20+
However, you may also want to configure a few additional components of Laravel, such as:
21+
22+
- [Cache](/docs/cache#configuration)
23+
- [Database](/docs/database#configuration)
24+
- [Queue](/docs/queues#configuration)
25+
- [Session](/docs/session#configuration)
26+
27+
> **Note:** You should never have the `APP_DEBUG` configuration option set to `true` for a production application.
28+
29+
<a name="permissions"></a>
30+
### Permissions
31+
32+
Laravel may require some permissions to be configured: folders within `storage` and the `bootstrap/cache` directory require write access by the web server.
33+
34+
<a name="configuration-files"></a>
35+
## Configuration Files
36+
37+
By default, Lumen uses a single `.env` file to configure your application. However, you may use full, "Laravel style" configuration files if you wish. The default configuration files are stored in `vendor/laravel/lumen/config` directory. Lumen will use your copy of the configuration file if you copy and paste one of the files into a `config` directory within your project root.
38+
39+
Using full configuration files will give you more control over some aspects of Lumen's configuration, such as configuring multiple storage "disks" or read / write database connections.
40+
41+
#### Custom Configuration Files
42+
43+
You may also create your own custom configuration files and load them using the `$app->configure()` method. For example, if your configuration file is located in `config/options.php`, you can load the file like so:
44+
45+
$app->configure('options');
46+
47+
<a name="pretty-urls"></a>
48+
## Pretty URLs
49+
50+
### Apache
51+
52+
The framework ships with a `public/.htaccess` file that is used to allow URLs without `index.php`. If you use Apache to serve your Laravel application, be sure to enable the `mod_rewrite` module.
53+
54+
If the `.htaccess` file that ships with Laravel does not work with your Apache installation, try this one:
55+
56+
Options +FollowSymLinks
57+
RewriteEngine On
58+
59+
RewriteCond %{REQUEST_FILENAME} !-d
60+
RewriteCond %{REQUEST_FILENAME} !-f
61+
RewriteRule ^ index.php [L]
62+
63+
If your web host doesn't allow the `FollowSymlinks` option, try replacing it with `Options +SymLinksIfOwnerMatch`.
64+
65+
### Nginx
66+
67+
On Nginx, the following directive in your site configuration will allow "pretty" URLs:
68+
69+
location / {
70+
try_files $uri $uri/ /index.php?$query_string;
71+
}
72+
73+
Of course, when using [Homestead](/docs/master/homestead), pretty URLs will be configured automatically.

0 commit comments

Comments
 (0)