Laravel Artisan Generator Command: The make:policy Command

December 7, 2016 —John Koster

The make:policy command is used to generate to policy classes. It requires a name to be provided; this name will be used as the file name as well as the newly generated class name. Policy classes are stored within the app/Policies directory.

The following example demonstrates how to use the make:policy command:

1# Generate a new policy class.
2php artisan make:policy DrinksPolicy

A newly created app/Policies/DrinksPolicy.php file will be created and will look similar to the following:

1<?php
2 
3namespace App\Policies;
4 
5use Illuminate\Auth\Access\HandlesAuthorization;
6 
7class DrinksPolicy
8{
9 use HandlesAuthorization;
10 
11 /**
12 * Create a new policy instance.
13 *
14 * @return void
15 */
16 public function __construct()
17 {
18 //
19 }
20}

This command will not overwrite any existing policy classes that have the same name. An error stating Policy already exists! will be displayed instead.

The make:policy command is also capable of generated namespaced classes. Accomplish this by separating namespace sections using the \ when supplying the policy name. A nested directory will be created and the correct namespace will be set on the generated class.

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.