-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[9.x] Controller middleware without resolving controller (#44516)
* controller middleware without resolving controller * update logic * add tests * Apply fixes from StyleCI Co-authored-by: StyleCI Bot <bot@styleci.io>
- Loading branch information
1 parent
5fb31a5
commit 6cdf03e
Showing
5 changed files
with
157 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing\Controllers; | ||
|
||
interface HasMiddleware | ||
{ | ||
/** | ||
* Get the middleware that should be assigned to the controller. | ||
* | ||
* @return \Illuminate\Routing\Controllers\Middleware|array | ||
*/ | ||
public static function middleware(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Illuminate\Routing\Controllers; | ||
|
||
use Closure; | ||
use Illuminate\Support\Arr; | ||
|
||
class Middleware | ||
{ | ||
/** | ||
* The middleware that should be assigned. | ||
* | ||
* @var \Closure|string|array | ||
*/ | ||
public $middleware; | ||
|
||
/** | ||
* The controller methods the middleware should only apply to. | ||
* | ||
* @var array|null | ||
*/ | ||
public $only; | ||
|
||
/** | ||
* The controller methods the middleware should not apply to. | ||
* | ||
* @var array|null | ||
*/ | ||
public $except; | ||
|
||
/** | ||
* Create a new controller middleware definition. | ||
* | ||
* @param \Closure|string|array $middleware | ||
* @return void | ||
*/ | ||
public function __construct(Closure|string|array $middleware) | ||
{ | ||
$this->middleware = $middleware; | ||
} | ||
|
||
/** | ||
* Specify the only controller methods the middleware should apply to. | ||
* | ||
* @param array|string $only | ||
* @return $this | ||
*/ | ||
public function only(array|string $only) | ||
{ | ||
$this->only = Arr::wrap($only); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Specify the controller methods the middleware should not apply to. | ||
* | ||
* @param array|string $only | ||
* @return $this | ||
*/ | ||
public function except(array|string $except) | ||
{ | ||
$this->except = Arr::wrap($except); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Illuminate\Tests\Integration\Routing; | ||
|
||
use Illuminate\Routing\Controllers\HasMiddleware; | ||
use Illuminate\Routing\Controllers\Middleware; | ||
use Illuminate\Support\Facades\Route; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class HasMiddlewareTest extends TestCase | ||
{ | ||
public function test_has_middleware_is_respected() | ||
{ | ||
$route = Route::get('/', [HasMiddlewareTestController::class, 'index']); | ||
$this->assertEquals($route->controllerMiddleware(), ['all', 'only-index']); | ||
|
||
$route = Route::get('/', [HasMiddlewareTestController::class, 'show']); | ||
$this->assertEquals($route->controllerMiddleware(), ['all', 'except-index']); | ||
} | ||
} | ||
|
||
class HasMiddlewareTestController implements HasMiddleware | ||
{ | ||
public static function middleware() | ||
{ | ||
return [ | ||
new Middleware('all'), | ||
(new Middleware('only-index'))->only('index'), | ||
(new Middleware('except-index'))->except('index'), | ||
]; | ||
} | ||
|
||
public function index() | ||
{ | ||
// | ||
} | ||
|
||
public function show() | ||
{ | ||
} | ||
} |