Skip to content
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
15 changes: 13 additions & 2 deletions config/route-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,21 @@
*/
],

/**
/*
* This middleware will be applied to all routes.
*/
'middleware' => [
\Illuminate\Routing\Middleware\SubstituteBindings::class
]
],

/*
* When enabled, implicitly scoped bindings will be enabled by default.
* You can override this behaviour by using the `ScopeBindings` attribute, and passing `false` to it.
*
* Possible values:
* - null: use the default behaviour
* - true: enable implicitly scoped bindings for all routes
* - false: disable implicitly scoped bindings for all routes
*/
'scope-bindings' => null,
];
2 changes: 1 addition & 1 deletion src/ClassRouteAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public function scopeBindings(): ?bool
{
/** @var ScopeBindings $attribute */
if (! $attribute = $this->getAttribute(ScopeBindings::class)) {
return null;
return config('route-attributes.scope-bindings');
}

return $attribute->scopeBindings;
Expand Down
28 changes: 28 additions & 0 deletions tests/AttributeTests/ScopeBindingsAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\BindingScoping1TestController;
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\BindingScoping2TestController;
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\BindingScoping3TestController;
use Spatie\RouteAttributes\Tests\TestClasses\Controllers\BindingScoping4TestController;

class ScopeBindingsAttributeTest extends TestCase
{
Expand Down Expand Up @@ -84,4 +85,31 @@ public function it_can_enable_binding_scoping_on_individual_methods_of_a_control
preventsScopedBindings: false
);
}


/** @test */
public function the_registrar_respects_default_scope_bindings_setting_from_config()
{
config()->set('route-attributes.scope-bindings', true);

$this->routeRegistrar->registerClass(BindingScoping4TestController::class);

$this
->assertRegisteredRoutesCount(2)
->assertRouteRegistered(
BindingScoping4TestController::class,
controllerMethod: 'index',
uri: 'default-scoping',
enforcesScopedBindings: true,
preventsScopedBindings: false
)
->assertRouteRegistered(
BindingScoping4TestController::class,
controllerMethod: 'store',
httpMethods: 'post',
uri: 'explicitly-disabled-scoping',
enforcesScopedBindings: false,
preventsScopedBindings: true
);
}
}
27 changes: 27 additions & 0 deletions tests/TestClasses/Controllers/BindingScoping4TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Spatie\RouteAttributes\Tests\TestClasses\Controllers;

use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\ScopeBindings;

final class BindingScoping4TestController
{

#[Get('default-scoping')]
public function index()
{

}

#[ScopeBindings(false)]
#[Post('explicitly-disabled-scoping')]
public function store()
{

}

}