Reputation: 212
So, I've got a question that, while based in Laravel, might just end up being something basic in PHP that I either missed or forgot. Or it may just turn out there is no "textbook/clean/standard" way of doing it.
A Question of Clutter
Anyway, I'm creating a fair number of Form Requests in Laravel (it's a sizable "app") and I'm constantly struck by how unnecessarily redundant their inclusion can be:
<?php
namespace App\Http\Controllers\Account;
use App\Http\Requests\SubscriberRequest;
use App\Http\Requests\SubscriberUpdateRequest;
use App\Http\Requests\SubscriberExtRequest;
use App\Http\Requests\SubscriberExtUpdateRequest;
class MailingListController extends BaseController
{
public function store(SubscriberRequest)
{
// code here
}
public function update(SubscriberUpdateRequest)
{
// code here
}
public function ExtCreate(SubscriberExtRequest)
{
// code here
}
public function ExtUpdate(SubscriberExtUpdateRequest)
{
// code here
}
}
The Alternative
Putting it up above moves the clutter out of the controller itself, but ends up being more work to create and maintain. I'm finding it's easier to just do it like this:
<?php
namespace App\Http\Controllers\Account;
class MailingListController extends BaseController
{
public function store(\App\Http\Requests\SubscriberRequest)
{
// code here
}
public function update(\App\Http\Requests\SubscriberUpdateRequest)
{
// code here
}
public function ExtCreate(\App\Http\Requests\SubscriberExtRequest)
{
// code here
}
public function ExtUpdate(\App\Http\Requests\SubscriberExtUpdateRequest)
{
// code here
}
}
The Question
Obviously, either of these ways is "fine" and "valid." This is hardly a high-priority issue. But the form requests are all coming from the same namespace. And when I'm working with a couple dozen controllers, the typing/copying can get tedious.
So, with all that in mind, is there an "ideal" PHP class-based solution to this problem? And, failing that, should I resort to an app-wide constant/variable? (Or is there some other trick anyone would recommend?)
Upvotes: 2
Views: 544
Reputation: 783
If you are using php 7+
Here is a nice way to keep your namespace inclusion cleaner.
use some\namespace\{ClassA, ClassB, ClassC as C};
Upvotes: 5