To address your requirement of integrating Companies House API checks into your form submissions while maintaining the flexibility for marketers to build and manage forms, you can indeed create a small Laravel application that serves as a middleware for your forms. This application will handle the API integration and form validation based on your business rules (e.g., company type and trading duration), and then pass the data back to your main WordPress site or directly to HubSpot.
Here’s a step-by-step approach to building this solution:
Step 1: Set Up Your Laravel Project
First, create a new Laravel project which will act as the middleware for handling the forms.
composer create-project --prefer-dist laravel/laravel laravelFormMiddleware
Step 2: Create a Form Model and Controller
You'll need a model to handle your form data and a controller to manage the business logic.
php artisan make:model Form -m
php artisan make:controller FormController
In the migration file that is generated (database/migrations/xxxx_xx_xx_xxxxxx_create_forms_table.php), add the necessary fields that you expect from the form.
Schema::create('forms', function (Blueprint $table) {
$table->id();
$table->string('company_name');
$table->string('registration_number');
// Add other fields as necessary
$table->timestamps();
});
Run the migration:
php artisan migrate
Step 3: Implement Companies House API Integration
In your FormController, add the logic to perform the API call to Companies House to validate the company details.
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class FormController extends Controller
{
public function checkCompany(Request $request)
{
$client = new Client();
$response = $client->request('GET', 'https://api.companieshouse.gov.uk/company/'.$request->registration_number, [
'headers' => [
'Authorization' => 'Basic '.base64_encode('your_api_key')
]
]);
$data = json_decode($response->getBody()->getContents(), true);
// Implement your business logic to check company type and trading years
if ($data['type'] != 'limited' || $data['trading_years'] < required_years) {
return response()->json(['error' => 'Invalid company type or trading duration'], 422);
}
// Save form data or pass it to another service
// Form::create($request->all());
return response()->json(['success' => 'Form submitted successfully']);
}
}
Step 4: Route Setup
Define a route in routes/web.php that will handle the form submission.
Route::post('/submit-form', [FormController::class, 'checkCompany']);
Step 5: Linking with WordPress
On your WordPress site, you can modify the form action URLs to point to the Laravel application’s endpoint (https://myapp.something/submit-form). This way, when the form is submitted, it first goes through the Laravel middleware for validation.
Step 6: Handling CORS
If your Laravel app and WordPress site are on different domains, you'll need to handle Cross-Origin Resource Sharing (CORS) in your Laravel application.
You can do this by modifying the cors.php config file in Laravel.
'paths' => ['api/*', 'submit-form'],
'allowed_origins' => ['https://yourwordpresssite.com'],
Conclusion
This setup allows you to intercept form submissions, perform necessary validations using the Companies House API, and then proceed with the form data as required. It integrates smoothly without taking away the ability of marketers to manage form creation and design, as they can still use the familiar tools provided by HubSpot or WordPress.