Skip to content

✨ Add & Setup Filament Admin #129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://laravel.cm.test
FRONTEND_APP_URL=http://localhost::4200
FILAMENT_PATH=cp
FRONTEND_APP_URL=http://localhost:4200

LOG_CHANNEL=stack
LOG_LEVEL=debug
Expand Down
40 changes: 19 additions & 21 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use App\Traits\HasSettings;
use App\Traits\HasUsername;
use App\Traits\Reacts;
use Filament\Models\Contracts\FilamentUser;
use Filament\Models\Contracts\HasAvatar;
use Filament\Models\Contracts\HasName;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -32,7 +35,7 @@
/**
* @mixin IdeHelperUser
*/
class User extends Authenticatable implements MustVerifyEmail, HasMedia, FeaturableInterface
class User extends Authenticatable implements MustVerifyEmail, HasMedia, FeaturableInterface, FilamentUser, HasName, HasAvatar
{
use Gamify;
use HasFactory;
Expand All @@ -47,11 +50,6 @@ class User extends Authenticatable implements MustVerifyEmail, HasMedia, Featura
use Reacts;
use Featurable;

/**
* The attributes that are mass assignable.
*
* @var array<string>
*/
protected $fillable = [
'name',
'email',
Expand All @@ -74,34 +72,19 @@ class User extends Authenticatable implements MustVerifyEmail, HasMedia, Featura
'opt_in',
];

/**
* The attributes that should be hidden for arrays.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
];

/**
* The attributes that should be cast to native types.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime',
'settings' => 'array',
];

/**
* The accessors to append to the model's array form.
*
* @var string[]
*/
protected $appends = [
'profile_photo_url',
'roles_label',
Expand Down Expand Up @@ -179,6 +162,21 @@ public function isLoggedInUser(): bool
return $this->id === Auth::id();
}

public function canAccessFilament(): bool
{
return str_ends_with($this->email, '@laravel.cm') || $this->isModerator() || $this->isAdmin();
}

public function getFilamentName(): string
{
return $this->name;
}

public function getFilamentAvatarUrl(): ?string
{
return $this->profile_photo_url;
}

/**
* @return array{name: string, username: string, picture: string}
*/
Expand Down
30 changes: 19 additions & 11 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\View\Composers\TopContributorsComposer;
use App\View\Composers\TopMembersComposer;
use Carbon\Carbon;
use Filament\Facades\Filament;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\View;
Expand All @@ -41,23 +42,16 @@ public function boot(): void
$this->bootMacros();
$this->bootViewsComposer();
$this->bootEloquentMorphs();
$this->bootFilament();

ReplyResource::withoutWrapping();
}

public function registerBladeDirective(): void
{
Blade::directive('title', function ($expression) {
return "<?php \$title = $expression ?>";
});

Blade::directive('shareImage', function ($expression) {
return "<?php \$shareImage = $expression ?>";
});

Blade::directive('canonical', function ($expression) {
return "<?php \$canonical = $expression ?>";
});
Blade::directive('title', fn ($expression) => "<?php \$title = $expression ?>");
Blade::directive('shareImage', fn ($expression) => "<?php \$shareImage = $expression ?>");
Blade::directive('canonical', fn ($expression) => "<?php \$canonical = $expression ?>");
}

public function bootMacros(): void
Expand Down Expand Up @@ -91,4 +85,18 @@ public function bootEloquentMorphs(): void
'user' => User::class,
]);
}

public function bootFilament(): void
{
Filament::serving(function () {
Filament::registerTheme(
mix('css/filament.css'),
);
});

Filament::registerRenderHook(
'body.start',
fn (): string => Blade::render('@livewire(\'livewire-ui-modal\')'),
);
}
}
38 changes: 13 additions & 25 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Providers;

use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
Expand All @@ -12,13 +13,6 @@

final class RouteServiceProvider extends ServiceProvider
{
/**
* The path to the "home" route for your application.
*
* This is used by Laravel authentication to redirect users after login.
*
* @var string
*/
public const HOME = '/dashboard';

public function boot(): void
Expand All @@ -36,37 +30,31 @@ public function boot(): void
Route::middleware('web')
->namespace($this->namespace)
->group(base_path('routes/web.php'));

Route::middleware(['web', 'auth', 'role:moderator|admin'])
->namespace($this->namespace)
->prefix('cpanel')
->as('cpanel.')
->group(base_path('routes/cpanel.php'));
});

Route::macro('redirectMap', function ($map, $status = 302) {
Route::macro('redirectMap', function (array $map, int $status = 302) {
foreach ($map as $old => $new) {
Route::redirect($old, $new, $status)->name($old);
}
});
}

/**
* Configure the rate limiters for the application.
*
* @return void
*/
protected function configureRateLimiting(): void
{
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by((string) (optional($request->user())->id ?: $request->ip()));
});
RateLimiter::for(
name: 'api',
callback: fn (Request $request): Limit => Limit::perMinute(60)
->by(
(string) (optional($request->user())->id ?: $request->ip())
)
);
}

protected function routeBindings(): void
{
Route::bind('username', function (string $username) {
return \App\Models\User::findByUsername($username);
});
Route::bind(
key: 'username',
binder: fn (string $username): User => User::findByUsername($username)
);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"blade-ui-kit/blade-ui-kit": "^0.3.4",
"cyrildewit/eloquent-viewable": "^6.1",
"doctrine/dbal": "^3.6.4",
"filament/filament": "^2.0",
"filament/notifications": "^2.17.49",
"francescomalatesta/laravel-feature": "^3.0",
"graham-campbell/markdown": "^14.0",
Expand Down
Loading