Widget Index Route

Widget Index Route

In this tutorial, you will be adding a new Route/URL to your application.

From a Resourceful Routing perspective, this route will be an Index for your Widget resource type.

That means you can expect visiting the new URL to show you a listing of the Widget records in your database.

This guide will assume a Test Driven Developement (TDD) approach to this work.

This page is a work in progress.

These are the bare minimums you will need to create for an Index route:

  1. A route. A route can be defined using the Route facade in the routes/web.php file. Routing Documentation

  2. A controller. There are options, but I recommend using a single action / invokable controler. Single Action Controllers Documentation

  3. A view. Views Documentation

Beyond this, I would recommend adding some feature tests for the route. Relevant Testing Documentation

and also considering moving the logic from the controller into a Query class.

  1. Make a folder at app/Queries/{resourceName}

  2. Add a php class within it, e.g. app/Queries/Widgets/IndexQuery.php

  3. Add a public method to the class to handle the processing. The name could be anything. getData() is a good options.

  4. Move the processing from the controller into this class.

  5. Return the results from the method;

  6. 'Catch' the returned results in the controller class and pass them to the view

  7. To get access to the query in the controller, you can use dependancy injection. Documeation about DI