Routes
Overview
The routes folder in a Laravel project contains all the route definitions that direct HTTP requests to the appropriate controller methods or closures. This is where we define the various endpoints of our application and how they should be handled.
Key Files used in Ecom-API
api.php
The api.php file within the routes folder defines several conditional and grouped routes. These routes are primarily used for API endpoints and are organized based on configuration settings.
- Key Elements in
api.php- Conditional Routes:
- The file defines several routes that are conditionally exposed based on configuration settings in the
config/app.phpfile. - Cache Test Endpoint:
if (config('app.expose_cache_test_endpoint')) { Route::get('/cache-test', 'CacheTestController'); }- This route exposes the
/cache-testendpoint, mapped to theCacheTestControllerifexpose_cache_test_endpointis set totruein the configuration.
- This route exposes the
- Once Test Endpoint:
if (config('app.expose_once_test_endpoint')) { Route::get('/once-test', 'OnceTestController'); }- This route exposes the
/once-testendpoint, mapped to theOnceTestControllerifexpose_once_test_endpointis enabled.
- This route exposes the
- Exception Test Endpoint:
if (config('app.expose_exception_test_endpoint')) { Route::get('/exception', 'ExceptionTestController'); }- This route exposes the
/exceptionendpoint, mapped to theExceptionTestControllerbased on theexpose_exception_test_endpointconfiguration.
- This route exposes the
- Health Check Route:
Route::get('/health-check', 'PingController');- This route is always available and is used for health checks, mapped to the
PingController.
- Grouped Routes with Optional Basic Authentication:
Route::middleware(config('auth.basic_auth.enabled') ? 'auth.basic.api' : [])->group(base_path('routes/endpoints.php'));- This section groups routes defined in the
routes/endpoints.phpfile. - It conditionally applies basic authentication middleware (
auth.basic.api) if theauth.basic_auth.enabledconfiguration is set totrue. If not enabled, it proceeds without applying any middleware.
- The file defines several routes that are conditionally exposed based on configuration settings in the
- Conditional Routes:
console.php
The console.php file is used to define custom Artisan commands for Laravel's command-line interface (CLI). These commands can perform various tasks, such as managing database records or automating specific processes.
- Key Elements in
console.php- Custom Artisan Command:
- The file defines a custom Artisan command named
submit-outstanding-orders-for-settlement. - This command is responsible for identifying outstanding orders that are authorized but not yet captured and submitting them for settlement.
- The file defines a custom Artisan command named
- Database Queries:
- The command interacts with two tables in the
paymentdatabase connection:toolstation_payment_se.transactionsandtoolstation.user_orders. - Query 1:
- The first query selects records from
toolstation_payment_se.transactionsjoined with thetoolstation.user_orderstable. - It filters for orders with specific criteria, such as being authorized (
status = 'authorised'), having no captured amount (amount_captured = 0), using a specific payment gateway (gateway = 3), and having a specific transaction type (transaction_type = 1). - It processes these records in chunks of 100, ordering them by the
uor_tstimestamp. - For each order, it dispatches a job to capture the payment using the
CapturePaymentForOrderjob.
- The first query selects records from
- Query 2:
- The second query is similar but operates on
toolstation_payment_se.transactionsjoined with thetoolstation.user_parent_orderstable. - It filters and processes records with similar criteria and orders them by the
uop_tstimestamp. - For each parent order, it retrieves the related order and dispatches the
CapturePaymentForOrderjob to capture the payment, forcing the capture if necessary.
- The second query is similar but operates on
- The command interacts with two tables in the
- Processing in Chunks:
- The
chunk(100, function ($rows) { ... })method is used to process the database records in chunks of 100 at a time. This approach is memory-efficient and helps prevent timeouts or memory issues when processing large datasets.
- The
- Job Dispatching:
- For each order identified by the queries, a job (
CapturePaymentForOrder) is dispatched to handle the payment capture process. This job is responsible for interacting with the payment gateway and ensuring that the payment is captured.
- For each order identified by the queries, a job (
- Custom Artisan Command:
endpoint.php
The endpoints.php file is responsible for defining various API routes that are grouped together under specific middleware, as referenced in the api.php file.
This includes multiple endpoints such as:
- Customer
- Accounts
- Content
- Orders
- Saved list, etc
- Read all the endpoints here