---
title: "Controllers"
canonical_url: "https://handbook.propay.co.za/laravel/controllers"
---
# Controllers

## Names

Controllers that control a resource must use the singular resource name.

<code-group>

```php [PHP]
// GOOD
final class CourseController {}

// BAD
final class CoursesController {}
```

</code-group>

## Defaults

Try to keep controllers simple and stick to the default CRUD keywords (`index`, `create`, `store`, `show`, `edit`, `update` and `destroy`). Extract a new controller if you need other actions. [Laravel Documentation](https://laravel.com/docs/12.x/controllers#actions-handled-by-resource-controllers)

<table>
<thead>
  <tr>
    <th>
      Verb
    </th>
    
    <th>
      URI
    </th>
    
    <th>
      Action
    </th>
    
    <th>
      Route Name
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos
      </code>
    </td>
    
    <td>
      index
    </td>
    
    <td>
      photos.index
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/create
      </code>
    </td>
    
    <td>
      create
    </td>
    
    <td>
      photos.create
    </td>
  </tr>
  
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        /photos
      </code>
    </td>
    
    <td>
      store
    </td>
    
    <td>
      photos.store
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      show
    </td>
    
    <td>
      photos.show
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/{photo}/edit
      </code>
    </td>
    
    <td>
      edit
    </td>
    
    <td>
      photos.edit
    </td>
  </tr>
  
  <tr>
    <td>
      PUT/PATCH
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      update
    </td>
    
    <td>
      photos.update
    </td>
  </tr>
  
  <tr>
    <td>
      DELETE
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      destroy
    </td>
    
    <td>
      photos.destroy
    </td>
  </tr>
</tbody>
</table>

This is a loose guideline that doesn’t need to be enforced, but keeping to the standards of Laravel is always a good idea.

## Method Injection

Use method injection for Request and other dependencies

<code-group>

```php [PHP]
// GOOD
public function update(Request $request, Course $course)
{
    $this->validate($request, ['email' => ['email']]);
    $name = $request->input('name');
}

// BAD
public function update(Course $course)
{
    $this->validate(request(), ['email' => ['email']]);
    $name = request('name');
}
```

</code-group>

## Param Order

Inject route params, then other dependencies

<code-group>

```php [PHP]
// GOOD
public function update(Team $team, Request $request, DetachFromTeamToIndividualGracePeriodAction $detachAction)
{
    ...
}

// BAD
public function update(Request $request, Team $team, DetachFromTeamToIndividualGracePeriodAction $detachAction)
{
    ...
}
```

</code-group>

The same applies to scalar GET params (good example: `public function update(int $teamId, Request $request)`).


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
