Controllers and Middleware
Every route uses a Controller to handle the request, and sometimes it's useful to automatically handle some things that occur during the request, before it hits your controller. (Middleware)
Supplied Middleware
The Dragon Framework supplies one piece of middleware, Dragon\Http\Middleware\ValidatesNonce, and it does exactly what it says: validates the nonce. When you use the @nonce Blade directive in a form, it'll produce a hidden form field that contains the nonce generated by WordPress. This middleware validates the nonce, and sets the following pieces of information on the request passed to your controller.
- Admin notice - It will set the admin notice with
wp_admin_notice(). WordPress will automatically show the notice above your page, so no need to add it to your template. nonce_invalid- It will betruewhen the nonce is invalid, orfalsefor a valid nonce. Feel free to react to that by checking$request->attributes->get('nonce_invalid').
Controllers
So how do you hook up a controller in Dragon Framework? Nearly the same way you do it in Laravel. If you're creating an admin page, extend the Dragon\Http\Controllers\AdminPageController base controller. For shortcodes, use Dragon\Http\Controllers\ShortcodeController. If you want to create a controller that isn't using a virtualized route, and thus will not show the WordPress page layout and only your code (like with WooCommerce's store setup screen), then use Illuminate\Routing\Controller directly.
Now, just configure your route the way you usually do in Laravel. If you're virtualizing a route, add a name() to your route so you can set that on the property for your controller using protected static string $routeName = "";.
How do I...?
Are you looking for more information on adding admin pages with their menu data? See the how to article on configuring admin menus.
Maybe you're looking for more information on creating a shortcode? Check out the walkthrough on building one.