---
url: /custom-modules/resources.md
---

# CRUD Resources

Evolve comes with powerful base classes for controllers and Livewire components.

## Resource Controllers

Resource controllers are normal Laravel controllers which extend Evolve's `ResourceController` class.

When defining the associated model as a `protected $modelName` property, this controller provides all restfull actions like `create`, `update` or `store`.

```php
use Racerfish\Blog\Models\Post;
use Racerfish\Evolve\Http\Controllers\Backend\ResourceController;

class PostController extends ResourceController
{
    protected $modelName = Post::class;
```

### Hooks

Controller hooks let you easily add custom logic into controller actions without having to override the full function.

The Resource Controller calls lifecycle hooks **before** and **after** performing data manipulation (Similar to Laravel's model events, but in the controller).

```php
public function saved(Model $item): void {
    $item->increment('edit_count');
    $item->save();
}
```

These hooks are available:

```php
public function saving(?Model $item): void
public function saved(Model $item): void

public function updating(Model $item): void
public function updated(Model $item): void

public function creating(): void
public function created(Model $item): void

public function deleting(Model $item): void
public function deleted(Model $item): void
```

### Validation & Authorization

Resource controllers pass all action calls through [FormRequest classes](https://laravel.com/docs/master/validation#form-request-validation) **if** they exist.

The controller looks for a class with a `[Model][Action]Request` naming convention under the same `Http\Requests\Backend` or `Http\Requests\Frontend` namespace (depending on the controllers namespace).

Example: For a controller handling a `Post` model, the `update` action will invoke a FormRequest called `Http\Requests\Backend\PostUpdateRequest`.

## Resource Managers

Resource managers are [Livewire](https://livewire.laravel.com/) components which extend Evolve's `ResourceComponent` class.

When defining the associated model as a `protected $modelName` property, the component provides a few "restfull" Livewire actions like `create`, `edit` or `delete`.

```php
use Racerfish\Blog\Models\Post;

class PostManager extends ResourceComponent
{
    protected $modelName = Post::class;
```

### Hooks

Similar to the Controller Hooks, the Resource Manager provides following hooks to add custom logic before and after a model is saved:

```php
public function savingItem($item)
public function savedItem($item)
```

## Views

Resource controllers and managers expect your views to have a path of `[packageName]::[modelName].[actionName]`.

As an example, you have a `Post` model in a module called `Blog`. The `index` controller action looks for a view at `blog::post.index` and passes all posts in a `$items` variable along.

### Customizing the view path

You can override the `getViewPath` function in your controller to implement your own way of how view paths are begin resolved.
