Skip to content

Commit

Permalink
Laravel 11: New Slim Base Controller (#958)
Browse files Browse the repository at this point in the history
  • Loading branch information
realodix authored Apr 19, 2024
1 parent 2bc95ef commit 6f449b1
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 34 deletions.
8 changes: 2 additions & 6 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

namespace App\Http\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
abstract class Controller
{
use AuthorizesRequests, ValidatesRequests;
//
}
9 changes: 6 additions & 3 deletions app/Http/Controllers/Dashboard/AboutSystemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
use App\Models\User;
use App\Models\Visit;
use App\Services\KeyGeneratorService;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};

class AboutSystemController extends Controller
class AboutSystemController extends Controller implements HasMiddleware
{
public function __construct()
public static function middleware(): array
{
$this->middleware('role:admin');
return [
new Middleware('role:admin'),
];
}

/**
Expand Down
12 changes: 6 additions & 6 deletions app/Http/Controllers/Dashboard/AllUrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

use App\Http\Controllers\Controller;
use App\Models\Url;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};

class AllUrlController extends Controller
class AllUrlController extends Controller implements HasMiddleware
{
/**
* AllUrlController constructor.
*/
public function __construct()
public static function middleware(): array
{
$this->middleware('role:admin');
return [
new Middleware('role:admin'),
];
}

/**
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/Dashboard/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Url;
use App\Models\User;
use App\Services\KeyGeneratorService;
use Illuminate\Support\Facades\Gate;

class DashboardController extends Controller
{
Expand All @@ -32,7 +33,7 @@ public function view()
*/
public function edit(Url $url)
{
$this->authorize('updateUrl', $url);
Gate::authorize('updateUrl', $url);

return view('backend.edit', ['url' => $url]);
}
Expand Down Expand Up @@ -67,7 +68,7 @@ public function update(StoreUrl $request, Url $url)
*/
public function delete(Url $url)
{
$this->authorize('forceDelete', $url);
Gate::authorize('forceDelete', $url);

$url->delete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateUserPassword;
use App\Models\User;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Hash;

class ChangePasswordController extends Controller
Expand All @@ -19,7 +20,7 @@ class ChangePasswordController extends Controller
*/
public function view(User $user)
{
$this->authorize('view', $user);
Gate::authorize('view', $user);

return view('backend.user.changepassword', ['user' => $user]);
}
Expand All @@ -35,7 +36,7 @@ public function view(User $user)
*/
public function update(UpdateUserPassword $request, User $user)
{
$this->authorize('updatePass', $user);
Gate::authorize('updatePass', $user);

$user->password = Hash::make($request['new-password']);
$user->save();
Expand Down
17 changes: 9 additions & 8 deletions app/Http/Controllers/Dashboard/User/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
use App\Http\Controllers\Controller;
use App\Http\Requests\UpdateUserEmail;
use App\Models\User;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};
use Illuminate\Support\Facades\Gate;

class UserController extends Controller
class UserController extends Controller implements HasMiddleware
{
/**
* UserController constructor.
*/
public function __construct()
public static function middleware(): array
{
$this->middleware('role:admin')->only('view');
return [
new Middleware('role:admin', only: ['view']),
];
}

/**
Expand All @@ -36,7 +37,7 @@ public function view()
*/
public function edit(User $user)
{
$this->authorize('view', $user);
Gate::authorize('view', $user);

return view('backend.user.profile', ['user' => $user]);
}
Expand All @@ -52,7 +53,7 @@ public function edit(User $user)
*/
public function update(UpdateUserEmail $request, User $user)
{
$this->authorize('update', $user);
Gate::authorize('update', $user);

$user->email = $request->email;
$user->save();
Expand Down
15 changes: 8 additions & 7 deletions app/Http/Controllers/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
use App\Models\User;
use App\Models\Visit;
use App\Services\QrCodeService;
use Illuminate\Routing\Controllers\{HasMiddleware, Middleware};
use Illuminate\Support\Facades\Gate;

class UrlController extends Controller
class UrlController extends Controller implements HasMiddleware
{
/**
* UrlController constructor.
*/
public function __construct()
public static function middleware(): array
{
$this->middleware('urlhublinkchecker')->only('create');
return [
new Middleware('urlhublinkchecker', only: ['create']),
];
}

/**
Expand Down Expand Up @@ -71,7 +72,7 @@ public function showDetail(Url $url)
*/
public function delete(Url $url)
{
$this->authorize('forceDelete', $url);
Gate::authorize('forceDelete', $url);

$url->delete();

Expand Down

0 comments on commit 6f449b1

Please sign in to comment.