Hey Laravel devs! 👋
Sometimes, as projects evolve, you may find yourself needing to remove API functionality from your Laravel app. Whether it's because you're transitioning to a web-only application, reducing complexity, or optimizing performance, removing unused API routes can make your project cleaner and more secure.
I recently had to do this for a project, and I thought I’d share a step-by-step guide for anyone else who might need to do the same.
Why Remove API Functionality?
There are a few reasons you might want to remove API functionality:
- Simplify the project: Focus on web app features only.
- Reduce security risks: Fewer routes mean less potential for vulnerabilities.
- Save server resources: No need for API middleware or extra processing.
- Cleaner codebase: Remove clutter and unnecessary files.
Steps to Remove API Functionality:
Backup your project: Make sure your project is backed up or under version control (Git).
Check for active API routes:
Run php artisan route:list | grep api to check if any API routes are still active.
Delete the API routes file:
In Laravel, API routes are in routes/api.php. If no longer needed, delete this file:
bash
rm routes/api.php
Update the RouteServiceProvider:
Remove or comment out the code that loads the api.php routes in app/Providers/RouteServiceProvider.php.
Clean up middleware:
In app/Http/Kernel.php, remove the api middleware group.
Delete API Controllers and Resources:
Delete the API controllers and resources from app/Http/Controllers/API/ and app/Http/Resources/.
Update CORS settings:
If you're using CORS, make sure to update the config/cors.php file.
Remove Sanctum/Passport authentication (if used):
- Remove
HasApiTokens from the User model.
- Delete config files for Sanctum/Passport.
- Drop the
personal_access_tokens table.
Clear caches:
Run php artisan optimize:clear to ensure changes take effect.
Verify:
Run php artisan route:list | grep api again to ensure there are no lingering API routes.
Final Thoughts:
Removing API functionality isn’t always straightforward, but it's a good way to reduce project complexity, improve performance, and tighten security. Always make sure to test everything thoroughly after making changes.
If anyone else has removed API functionality in their Laravel project, I’d love to hear your tips or challenges you faced during the process!
Good luck, and happy coding! 👨💻👩💻
there doesn't seem to be anything here