F
Filament•3mo 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;
}
Solution:
Right that's why, you are using a panel page but not yet in panels as you are not logged in. That's your issue. Just use a custom Livewire page
Jump to solution
17 Replies
Jamie Cee
Jamie CeeOP•3mo 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•3mo 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
Jamie Cee
Jamie CeeOP•3mo ago
Sorry for the delay, got put on another project. So there is a function further up that defines $this->parameters, I should have shared that also, my bad. This is what the class is by default
class AuthorizationViewResponse implements \Laravel\Passport\Contracts\AuthorizationViewResponse
{
/**
* An array of arguments that may be passed to the view response and used in the view.
*
* @var string
*/
protected $parameters;

/**
* Add parameters to response.
*
* @param array $parameters
* @return $this
*/
public function withParameters($parameters = [])
{
$this->parameters = $parameters;

return $this;
}

/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
if (! is_callable($this->view) || is_string($this->view)) {
return response()->view($this->view, $this->parameters);
}

$response = call_user_func($this->view, $this->parameters);

if ($response instanceof Responsable) {
return $response->toResponse($request);
}

return $response;
}
}
class AuthorizationViewResponse implements \Laravel\Passport\Contracts\AuthorizationViewResponse
{
/**
* An array of arguments that may be passed to the view response and used in the view.
*
* @var string
*/
protected $parameters;

/**
* Add parameters to response.
*
* @param array $parameters
* @return $this
*/
public function withParameters($parameters = [])
{
$this->parameters = $parameters;

return $this;
}

/**
* Create an HTTP response that represents the object.
*
* @param \Illuminate\Http\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function toResponse($request)
{
if (! is_callable($this->view) || is_string($this->view)) {
return response()->view($this->view, $this->parameters);
}

$response = call_user_func($this->view, $this->parameters);

if ($response instanceof Responsable) {
return $response->toResponse($request);
}

return $response;
}
}
toeknee
toeknee•3mo ago
Try: self::parameters if that's defined further up.
Jamie Cee
Jamie CeeOP•3mo ago
Oh, I also missed out that the context of $this, was in my view, rather than the backend. So was when I was using $this->form in my blade view.. But also seems I didnt save this to a branch, so will have to add everything back in manually
toeknee
toeknee•3mo ago
So don't use $this-> in the view if you add it to the livewire blade you can just call $parameters
Jamie Cee
Jamie CeeOP•3mo ago
But I think because I also use the ->render() then it is going straight to the view, rather than the page class? Or am I dumb?
public function toResponse($request)
{
return (new OAuthAuthorizationPage($this->parameters))->render();
}
public function toResponse($request)
{
return (new OAuthAuthorizationPage($this->parameters))->render();
}
As mount() never gets picked up either Even if I have nothing in my blade other than the page
<x-filament-panels::page>
</x-filament-panels::page>
<x-filament-panels::page>
</x-filament-panels::page>
It still complains about $this. So there must be something in my page that im missing from the setup? I have a feeling its because its not finding an instance of the livewire component when im instantiating my page like the above. But there is a lack of resources surrounding this online or in the documentation, so I am unclear of what I need to do If it helps, my use case is that passport has its own oauth2 flow/screen. I want to change that screens styling to match my styling used in the filament cms
toeknee
toeknee•3mo ago
You shouldn't use $this in a custom page view.
Jamie Cee
Jamie CeeOP•3mo ago
So how can I have the ability to implement the filament-panels::page with the livewire components needed so I can get the styling?
toeknee
toeknee•3mo ago
So anything you define ont eh livewire component is accesible within the view. you just call it directly i.e. $parameters not $this->parameters
Jamie Cee
Jamie CeeOP•3mo ago
Yeah, but lets say I add nothing. So all my blade view is:
<x-filament-panels::page>
</x-filament-panels::page>
<x-filament-panels::page>
</x-filament-panels::page>
Im still getting the error
toeknee
toeknee•3mo ago
If you have opcache enabled you need retart php-fpm what is the livewire component for that page?
Jamie Cee
Jamie CeeOP•3mo ago
I dont think one was being passed, I wasnt sure how to set one up
toeknee
toeknee•3mo ago
So how is the page being defined/rendered?
Jamie Cee
Jamie CeeOP•3mo ago
https://discord.com/channels/883083792112300104/1341697229685002332/1341700838623805491 I was trying to define it through the oauth2 flow Think Im just going about all this in the wrong way, and confusing myself
Solution
toeknee
toeknee•3mo ago
Right that's why, you are using a panel page but not yet in panels as you are not logged in. That's your issue. Just use a custom Livewire page
Jamie Cee
Jamie CeeOP•2mo ago
Gotcha, cheers

Did you find this page helpful?