F
Filamentā€¢5d ago
Jamie Cee

Calling a custom page from a function

I am extending the default passport oauth2 flow and want to customize the view, in which I want to stick with filaments design. So I create a page class, and then in my function, I need to know how to return the page etc
public function toResponse($request)
{
return (new OAuthAuthorizationPage($this->parameters))->render();
}
public function toResponse($request)
{
return (new OAuthAuthorizationPage($this->parameters))->render();
}
I have this currently, and it goes to the blade file, but doesn't detect any of my variables which I set in the mount
public function mount(Request $request)
{
dd($request);
$this->clientName = $request->client->name ?? 'Unknown App';
$this->scopes = $request->scopes ?? [];
$this->state = $request->state;
$this->clientId = $request->client->getKey();
$this->authToken = $request->authToken;
}
public function mount(Request $request)
{
dd($request);
$this->clientName = $request->client->name ?? 'Unknown App';
$this->scopes = $request->scopes ?? [];
$this->state = $request->state;
$this->clientId = $request->client->getKey();
$this->authToken = $request->authToken;
}
2 Replies
Jamie Cee
Jamie CeeOPā€¢5d ago
If I try this approach
public function toResponse($request)
{
// return (new OAuthAuthorizationPage($this->parameters))->render();
// Merge the parameters into the request, so they're available in mount()
$request->merge($this->parameters);

// Instantiate the page using Filament's make() method, which ensures lifecycle methods like mount() are called
$page = new OAuthAuthorizationPage();

$page->mount($request);

// Render the page
return $page->render();
}
public function toResponse($request)
{
// return (new OAuthAuthorizationPage($this->parameters))->render();
// Merge the parameters into the request, so they're available in mount()
$request->merge($this->parameters);

// Instantiate the page using Filament's make() method, which ensures lifecycle methods like mount() are called
$page = new OAuthAuthorizationPage();

$page->mount($request);

// Render the page
return $page->render();
}
I get Using $this when not in object context My page
class OAuthAuthorizationPage extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.passport-authorize';

public ?string $clientName = null;
public ?array $scopes = [];
public ?string $state = null;
public ?string $clientId = null;
public ?string $authToken = null;

public function mount(Request $request)
{
$this->clientName = $request->client->name ?? 'Unknown App';
$this->scopes = $request->scopes ?? [];
$this->state = $request->state;
$this->clientId = $request->client->getKey();
$this->authToken = $request->authToken;
}

public function approve()
{
$response = redirect()->route('passport.authorizations.approve', [
'state' => $this->state,
'client_id' => $this->clientId,
'auth_token' => $this->authToken,
]);

Notification::make()->title('Access Granted')->success()->send();
return $response;
}

public function deny()
{
$response = redirect()->route('passport.authorizations.deny', [
'state' => $this->state,
'client_id' => $this->clientId,
'auth_token' => $this->authToken,
]);

Notification::make()->title('Access Denied')->warning()->send();
return $response;
}
class OAuthAuthorizationPage extends Page
{
protected static ?string $navigationIcon = 'heroicon-o-key';
protected static string $view = 'filament.pages.passport-authorize';

public ?string $clientName = null;
public ?array $scopes = [];
public ?string $state = null;
public ?string $clientId = null;
public ?string $authToken = null;

public function mount(Request $request)
{
$this->clientName = $request->client->name ?? 'Unknown App';
$this->scopes = $request->scopes ?? [];
$this->state = $request->state;
$this->clientId = $request->client->getKey();
$this->authToken = $request->authToken;
}

public function approve()
{
$response = redirect()->route('passport.authorizations.approve', [
'state' => $this->state,
'client_id' => $this->clientId,
'auth_token' => $this->authToken,
]);

Notification::make()->title('Access Granted')->success()->send();
return $response;
}

public function deny()
{
$response = redirect()->route('passport.authorizations.deny', [
'state' => $this->state,
'client_id' => $this->clientId,
'auth_token' => $this->authToken,
]);

Notification::make()->title('Access Denied')->warning()->send();
return $response;
}
protected function getFormSchema(): array
{
return [
Hidden::make('state')->default($this->state),
Hidden::make('client_id')->default($this->clientId),
Hidden::make('auth_token')->default($this->authToken),
];
}

protected function getActions(): array
{
return [
Action::make('approve')
->label('Authorize')
->color('success')
->action('approve')
->icon('heroicon-o-check'),

Action::make('deny')
->label('Cancel')
->color('danger')
->action('deny')
->icon('heroicon-o-x'),
];
}

public function getViewData(): array
{
return [
'clientName' => $this->clientName,
'scopes' => $this->scopes,
'state' => $this->state,
'clientId' => $this->clientId,
'authToken' => $this->authToken,
];
}
}
protected function getFormSchema(): array
{
return [
Hidden::make('state')->default($this->state),
Hidden::make('client_id')->default($this->clientId),
Hidden::make('auth_token')->default($this->authToken),
];
}

protected function getActions(): array
{
return [
Action::make('approve')
->label('Authorize')
->color('success')
->action('approve')
->icon('heroicon-o-check'),

Action::make('deny')
->label('Cancel')
->color('danger')
->action('deny')
->icon('heroicon-o-x'),
];
}

public function getViewData(): array
{
return [
'clientName' => $this->clientName,
'scopes' => $this->scopes,
'state' => $this->state,
'clientId' => $this->clientId,
'authToken' => $this->authToken,
];
}
}
šŸ‘‹
toeknee
toekneeā€¢5d ago
$this means it doesn't exist in the method you are using it. toResponse wouldn't have $this->parameters You need to work out what $this->parameters is

Did you find this page helpful?