Skip to content

Instantly share code, notes, and snippets.

@chasecmiller
Created June 29, 2023 00:17
Show Gist options
  • Save chasecmiller/ff18a555b9f0f7fed463c88dd087f23d to your computer and use it in GitHub Desktop.
Save chasecmiller/ff18a555b9f0f7fed463c88dd087f23d to your computer and use it in GitHub Desktop.
Adding middleware to specific resource pages in Filament.

Adding middleware to specific resource pages in Filament.

Add this code to your resource class.

/**
 * Bring our routes online.
 * @return Closure
 */
public static function getRoutes(): Closure	{
	return function () {
		$slug = static::getSlug();
		Route::name("{$slug}.")
			->prefix($slug)
			->middleware(static::getMiddlewares())
			->group(function () {
				foreach (static::getPages() as $name => $page) {
					Route::get($page['route'], $page['class'])
						->name($name)
						->middleware(array_key_exists('middleware', $page) ? $page['middleware'] : null);
				}
			});
	};
}

Add this code to the resource pages you wish to add unique middleware to.

/**
 * Overriding the parent method to add a middleware attribute.
 * @param string $path
 * @return string[]
 */
public static function route(string $path): array {
	return [
		'class' => static::class,
		'route' => $path,
		'middleware' => [
			'password.confirm'
		]
	];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment