Installation:
composer require muhaiminshihab/ctaroutes
Publish the config (if needed) via:
php artisan vendor:publish --provider="MuhaiminShihab\CtaRoutes\CtaRoutesServiceProvider"
First Use Case: Access the package’s routes directly in your browser:
GET /cta-routes/cache-clearGET /cta-routes/migrateGET /cta-routes/storage-linkVerify the route works by checking the response (e.g., 200 OK with Artisan command output).
Where to Look First:
config/ctaroutes.php (if published) for customization (e.g., route prefixes, middleware).app/Providers/CtaRoutesServiceProvider.php to extend or override routes.php artisan route:list | grep cta-routes to inspect available endpoints.Browser-Based Artisan Commands: Use the package to expose Artisan commands as HTTP routes for non-technical users (e.g., QA, DevOps) or automated workflows (e.g., CI/CD hooks). Example:
// Trigger via browser or API client
$response = Http::get('/cta-routes/migrate');
Middleware Integration:
Restrict access to specific routes using Laravel middleware (e.g., auth, admin):
// In CtaRoutesServiceProvider.php
Route::get('/cta-routes/migrate', [CtaRoutesController::class, 'migrate'])
->middleware(['auth', 'can:run-migrations']);
Custom Routes: Extend the package by adding new routes in the service provider:
// Add a custom route for `queue:work`
Route::get('/cta-routes/queue-work', function () {
Artisan::call('queue:work');
return response()->json(['status' => 'Queue worker started']);
});
API Endpoints: Wrap routes in API resources for programmatic access:
Route::prefix('api/cta')->group(function () {
Route::get('/migrate', [CtaRoutesController::class, 'migrate']);
});
Workflow Automation: Chain routes in a deployment script or CI pipeline:
curl -X GET http://your-app.test/cta-routes/migrate-fresh
curl -X GET http://your-app.test/cta-routes/config-cache
config/ctaroutes.php for production if unsafe:
'enabled' => env('APP_ENV') !== 'production',
// In CtaRoutesController.php
Artisan::call('migrate');
Log::info('Migrations ran via CtaRoutes', ['output' => Artisan::output()]);
throttle middleware:
Route::get('/cta-routes/migrate', ...)->middleware('throttle:10,1');
Permission Issues:
storage:link route may fail if the storage/app/public directory lacks write permissions. Fix with:
chmod -R 775 storage/
migrate:fresh) require database access. Ensure your Laravel app’s .env is configured correctly.Route Conflicts:
/cta-routes/, but ensure no existing routes or packages (e.g., laravel-debugbar) conflict. Check with:
php artisan route:list
Output Handling:
STDOUT by default. For silent execution, suppress output:
Artisan::call('cache:clear', [], null, null, true); // 5th param = silent
Database Transactions:
migrate:fresh drop tables without rolling back. Test in a staging environment first.Caching Side Effects:
config:cache or cache:clear may cause unexpected behavior if misused (e.g., clearing cache mid-request). Use sparingly in production.Check Artisan Output: Redirect output to a file for debugging:
Artisan::call('migrate', [], function ($output) {
file_put_contents(storage_path('logs/ctaroutes.log'), $output);
});
Verify Middleware:
Ensure middleware (e.g., web or api) is applied to routes. Add ->middleware('web') if needed.
Test Locally:
Use Laravel’s --env=testing flag to avoid unintended side effects:
php artisan --env=testing cta-routes:migrate
Custom Commands:
Extend the package by adding new routes in CtaRoutesServiceProvider.php:
Route::get('/cta-routes/custom-command', function () {
Artisan::call('your:custom-command');
});
Middleware Overrides: Override the default middleware for specific routes:
Route::get('/cta-routes/migrate', ...)->middleware('admin');
Response Formatting: Standardize responses by wrapping Artisan output in JSON:
return response()->json([
'success' => true,
'output' => Artisan::output(),
'command' => 'migrate'
]);
Environment-Specific Routes: Dynamically enable/disable routes based on environment:
if (app()->environment('local')) {
Route::get('/cta-routes/debug', ...);
}
Event Listeners: Trigger events after commands run (e.g., notify Slack):
event(new CtaRouteExecuted('migrate', Artisan::output()));
How can I help you explore Laravel packages today?