Controllers

Instead of defining all of your request handling logic as Closures in route files, you may wish to organize this behavior using Controller classes. Controllers can group related request handling logic into a single class. Controllers are stored in the app/controllers directory.

Defining Controllers

You can generate controller with:

php atom g:ler PostsController

or

php atom generate:controller PostsController

Below is an example of a basic controller class:

<?php

namespace App\Controllers;


use App\Models\Post;

class PostsController
{

    public function show($id)
    {
        $post = Post::find('id', $id);
        return view('posts.show', ['post' => $post]);
    }

}

You can define a route to this controller action like so:

router()->get('posts/:id', 'PostsController@show');

Now, when a request matches the specified route URI, the show method on the PostsController class will be executed. Of course, the route parameters will also be passed to the method.

Controllers & Namespaces

It is very important to note that we did not need to specify the full controller namespace when defining the controller route. Since the Router loads your route files within a route group that contains the namespace, we only specified the portion of the class name that comes after the App\Controllers portion of the namespace.

If you choose to nest your controllers deeper into the app\controllers directory, use the specific class name relative to the App\Controllers root namespace. So, if your full controller class is App\Controllers\Photos\AdminController, you should register routes to the controller like so:

router()->get('foo', 'Photos\AdminController@method');

Resource Controllers

Atom resources routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for "photos" stored by your application. Using the m:ler Atom command, we can quickly create such a controller:

php atom g:ler PhotoController --resource

This command will generate a controller at app/controllers/PhotoController.php. The controller will contain a method for each of the available resource operations.

This command will also generate a resourceful route for your controller:

router()->resource('photos', 'PhotoController');

This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.

Actions Handled By Resource Controller

Verb

URI

Action

Route Name

GET

/photos

index

photos.index

GET

/photos/create

create

photos.create

POST

/photos

store

photos.store

GET

/photos/:photo

show

photos.show

GET

/photos/:photo/edit

edit

photos.edit

POST

/photos/:photo

update

photos.update

POST

/photos/:photo/delete

destroy

photos.destroy

Specifying The Resource Model

If you are using route model binding and would like the resource controller's methods to type-hint a model instance, you may use the --model option when generating the controller:

php atom g:ler PhotoController --resource --model=Photo

Dernière mise à jour

Cet article vous a-t-il été utile ?