Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ferilagi-66341611's avatar

Inertiajs & Pennant

good solution using pennant to change ui in inertiajs

my case, im defining feature "new-ui" done
  • define which user affected with this features
user->betatester() 
  • new ui is full page component like Dashboard/Index Profile/Index other still use old ui
  • for now iam using if else in controller like in dashboard controller
 return Feature::active('new-ui')
                ? Inertia::render('New/Dashboard/Index', [$data]);
                :  Inertia::render('Dashboard/Index', [$data]);
  • but its not efficient if i have like 100 function and add this code 1 by 1
any good approach for my case?
  • when in folder New exist user assosiated with features 'new-ui' using this page but if not exist just use old ui
  • my new ui always use same as old ui folder structure but just in New Folder in Resource/js/Page/New/[sameAsOldUI]
0 likes
4 replies
martinbean's avatar

@ferilagi-66341611 If you want to invoke your Pennant feature at the render level, then you could extend Inertia’s response factory in the container with your own that overrides the render method, and prepends New/ to any component names if the feature is active:

use Inertia\ResponseFactory;

$this->app->extend(ResponseFactory::class, function (ResponseFactory $original) {
    return new CustomResponseFactory($original);
});
namespace App\Extensions\Inertia;

use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Inertia\Response;
use Inertia\ResponseFactory;
use Laravel\Pennant\Feature;

class CustomResponseFactory
{
    use ForwardsCalls;

    protected ResponseFactory $original;

    public function __construct(ResponseFactory $original)
    {
        $this->original = $original;
    }

    public function render(string $component, $props = []): Response
    {
        if (Feature::active('new-ui')) {
            $component = Str::start($component, 'New/');
        }

        return $this->original->render($component, $props);
    }

    public function __call($method, $parameters)
    {
        return $this->forwardCallTo($this->original, $method, $parameters);
    }
}
1 like
ferilagi-66341611's avatar

thanks @martinbean, ill try with your method first, but i dont see the fallback method when using new-ui features if component / page in Folder New doesn't exist, just using old component in folder page

Snapey's avatar

As a related comment, I would totally avoid using 'new' for your revised design.

Becomes a mess in the future when you have 'new' folders that are no longer new.

I would start by moving files to be replaced using an 'outgoing' label, fix controllers etc and test it all works, then add the penant switch and create the new view using the original name. When you are happy with the feature you can remove the 'outgoing' files, and not mess with the files you recently created.

3 likes

Please or to participate in this conversation.