Managing Multiple API versions in Laravel - laravel

I'm working on exposing an API using one base URL (e.g. https://api.domain.com) and having that URL handle all versions of the API (the API consumer will need to send Accept-Version in the request header to indicate which version of the API they're trying to use).
How would I manage this approach in Laravel?
I was thinking that I could put all of my controllers, helpers, models, routes, config, etc. in a, say, 1.0.0 folder and use those for version 1 of my API. When I release a new version, maybe I make copies of the original code, put them in a 1.1.0 folder, and make changes there.
If I were to use this folder approach, what would I need to do in the routes to indicate which folder to use? How would my controllers know what models, helpers, config, etc. to use? Feels like this approach could get very messy and convoluted.

In your RouteServiceProvider.php you can define the routes for the application. Here you can also define the namespace, name prefix, and middleware for the routes. Simply add a new route map for each version of your api. Have the middleware check the version, redirecting if necessary, and set the name prefix/namespace to the correct version directory.
the RouteServiceProvider would look something like:
protected function mapApi-1-0-0-Routes()
{
Route::middleware(['api', 'version'])
->name('1.0.0.')
->namespace('1.0.0')
->group(base_path('routes/api.php'));
}
then your middleware would look something like
public function handle($request, Closure $next)
{
switch ($request->version) {
case "1.0.0":
$route = $request->version.$request->route()->getName();
break;
// more versions
return redirect()->route($route)->with($request);
}
I haven't tested this.
Route group name prefixes:
https://laravel.com/docs/7.x/routing#route-group-name-prefixes
Route namespaces:
https://laravel.com/docs/7.x/routing#route-group-namespaces
Redirecting named routes:
https://laravel.com/docs/7.x/redirects#redirecting-named-routes
Global middleware:
https://laravel.com/docs/7.x/middleware#global-middleware
Writing service providers:
https://laravel.com/docs/7.x/providers#writing-service-providers

Related

How to use old Laravel routing style in Laravel 8

I just installed Laravel 8 and in this version, I have to type my routes like this:
Route::get('/admin/panel', [App\Http\Controllers\Admin\PanelController::class, 'index']);
But I got used to Laravel 5 routes which looked like this:
Route::namespace('Admin')->prefix('admin')->group(function () {
Route::get('/panel', 'Admin/PanelController#index');
});
So how can I use this Laravel 5 routing inside Laravel 8 version?
If you're wanting to continue using the "older" way of defining a route (i.e. Controller#action) then you can do so but you need to alter the RouteServiceProvider to include the App\Http\Controllers namespace.
This is pretty straight forward and is with the more recent versions of Laravel 8 a simple case of uncommenting the following line:
protected $namespace = 'App\\Http\\Controllers';
If the version of Laravel 8 you're using doesn't have this line in the RouteServiceProvider file, you could upgrade your Laravel version or manually add it. If you manually add the line, you will also need to update the Route definitions in the boot method to use the $namespace property. Again, this is very straight forward, just add the following to the web and api definitions:
->namespace($this->namespace)
So for example:
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));
Then you should be good to go.
You can still use it to some extend (e.g. grouping and prefixing a route) but because you're using a FQCN the namespace is redundant. The major advantage using the "new" routing over of the "old" one is refactoring. When you move or rename your controller with an IDE that supports refactoring like PhpStorm you wouldn't need to manually change the name and group namespace of your controller.
In Laravel 5 you were able to use this route notation too but it wasn't outlined in the documentation.
To achieve a similar feeling, import the namespace and use just your class names and get rid of the namespace-group in the definition.
use App\Http\Controllers\Admin\PanelController;
Route::prefix('admin')->group(function () {
Route::get('panel', [PanelController::class, 'index']);
});
If you just cannot live with the new way of defining routes, uncomment the $namespace property in app/Providers/RouteServiceProvider.php and you're back to the old way.

Laravel route works with subdomain in 2 environments

I'm coding a platform to generate sub-websites.
I have a route like this who works very well in local :
//Website
Route::domain('{slug}.domain.test')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
I want to be able to make it works as well in production (other domain), so i did :
//Website
Route::domain('{slug}.{domain}')->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
And in my template :
Website
The generated URL looks amazing, but the routing doesn't work and bring me to the parent page of the main domain.
What i am doing wrong ?
Thanks
Working with domain routes like this is a little bit of a pain in Laravel.
In an application recently, I parsed the domain part from the application URL and then set it as a configuration value like this:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
config([
'app.domain' => parse_url(config('app.url'), PHP_URL_HOST),
]);
}
}
You can then use that in your domain routes:
Route::domain('{slug}.'.config('app.domain'), function () {
// Subdomain routes that work in all environments
});
If you are using laravel homestead, you'll need to register every subdomain on the etc/hosts file and on Homestead.yaml, as you probably did with the main domain. Now, I'll recommend the structure:
app.test
subdomain1.app.test
subdomain2.app.test
I wouldn't recommend to use something like:
subdomain1.app1.test
subdomain2.app2.test
I mean, you could, but it doesn't make much sense, and you would also have to register all of this on your Homestead/Local and Production environments.
For the approach that I suggest, you could set up this on an environment variable. https://laravel.com/docs/6.x/configuration#environment-variable-types
You can add any env variable you want/need to the .env file, there's an APP_URL variable, but this includes the http protocol, you could add something like:
APP_DOMAIN=app.test
And for production
APP_DOMAIN=yourapp.com
Then on the routes file access it with the helper method env, you can omit the second parameter, or use it to setup a default value, in case you forget to put it on the .env file.
Route::domain('{slug}.' . env('APP_DOMAIN', 'default.com'))->group(function () {
Route::get('/','WebsitesController#show')->name('web_website_show');
});
BTW, this might be of help for setting up your slug value with named routes:
Setting up named routes within a subdomain group in Laravel 5.7

Route::getRoutes() returns only package routes

I am trying to fetch all routes in Laravel package using \Illuminate\Support\Facades\Route::getRoutes();. But it gives only package routes, not the entire Laravel application routes.
Is there any way to fetch entire Laravel application routes inside the package.
You will need to load the application routes first in order to be able to list them. So in your package Service Provider, within the boot method, you can load the routes from the application like this:
public function boot()
{
$this->loadRoutesFrom(base_path('/routes/web.php')); // or /routes/api.php
}
Then you can use
Route::getRoutes();

Adding a route in Laravel as the last route, from a composer module?

I am making a module for Laravel, and for the ease of reusability, I am making it as a separate composer module.
In this module, I have to define a catch-all route, but I dont want it to override any of the manualy added routes, in the project.
Does anyone have a good idea how I can get this behaviour?
I am registering my route in the ServiceProviders boot() method like this:
public function boot()
{
$this->loadMigrationsFrom(__DIR__.'/migrations');
$this->loadRoutesFrom(__DIR__.'/routes/routes.php');
}
and the routes.php is also rather simple:
$regex = ".*";
Route::namespace('Asator\\Runepost\\Controllers')
->middleware(['web', DynamicContent::class])
->group(function($route) use ($regex) {
$route->any('{any}', 'RunepostFrontController')->where('any', $regex);
});
Is it possible to somehow add the route as the last route, after the manually added routes has run?
Just add your module's ServiceProvider after the App's RouteServiceProvider in config/app.php and make sure your catch-all-route is the last route of your module's routes.

Laravel Route to Standalone WebApp

I am trying to build a portal in Laravel to serve some other, standalone web apps (not built in Laravel), but I am struggling to find out how to route to these apps if I want to place them outside the public folder.
In the past, I would use (temporary) symlinks for this kind of things, but I was wondering if Laravel provides another solution.
So, I have a folder:
module
module/index.php
module/js/whatever
module/css/whatever
module/img/whatever
and I want a route /modules/1 to link to index.php in the module-folder in such a way that the resources in this folder (js/css/img) are also accessible.
Any suggestions?
You can include other PHP files with require_once.
web.php
Route::any('/webapp/{assets?}', 'WebAppController#index');
WebAppController
class WebAppController {
public function index(Request $request) {
require_once '../module/index.php';
if ($request->assets) {
// check from session if user is logged in
// require asset as well
// (or download them https://laravel.com/docs/5.5/responses#file-downloads)
}
}
}
http://php.net/manual/en/function.require-once.php

Resources