Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
|
@@ -64,6 +64,13 @@ class Gate implements GateContract | ||
*/ | */ | ||
protected $stringCallbacks = []; | protected $stringCallbacks = []; | ||
|
|
||
/** | |||
* The callback to be used to guess policy names. | |||
* | |||
* @var callable|null | |||
*/ | |||
protected $guessPolicyNamesUsingCallback; | |||
|
|||
/** | /** | ||
* Create a new gate instance. | * Create a new gate instance. | ||
* | * | ||
|
@@ -550,11 +557,28 @@ public function getPolicyFor($class) | ||
*/ | */ | ||
protected function guessPolicyName($class) | protected function guessPolicyName($class) | ||
{ | { | ||
if ($this->guessPolicyNamesUsingCallback) { | |||
return call_user_func($this->guessPolicyNamesUsingCallback, $class); | |||
} | |||
|
|||
$classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class))); | $classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class))); | ||
|
|
||
return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; | return $classDirname.'\\Policies\\'.class_basename($class).'Policy'; | ||
} | } | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
simonschaufi
|
|||
|
|
||
/** | |||
* Specify a callback to be used to guess policy names. | |||
* | |||
* @param callable $callback | |||
* @return $this | |||
*/ | |||
public function guessPolicyNamesUsing(callable $callback) | |||
{ | |||
$this->guessPolicyNamesUsingCallback = $callback; | |||
|
|||
return $this; | |||
} | |||
|
|||
/** | /** | ||
* Build a policy class instance of the given type. | * Build a policy class instance of the given type. | ||
* | * | ||
|
3 comments
on commit 150a63d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just look at the last few lines of the guessPolicyName
method for inspiration then assign your own callback to `guessPolicyNamesUsingCallback```.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An example would be nice for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this works for me @ludo237
public function boot()
{
Gate::guessPolicyNamesUsing(function ($class) {
$pre = str_replace('/', '\\', 'App\\Policies\\' .
class_basename(
str_replace('App\Models\\', '', $class)) . 'Policy');
// dd($pre, $class);
return $pre;
});
$this->registerPolicies();
//
}
If my models are in the
App\Models
namespace, does it mean I need to put the policies in theApp\Models\Policies
namespace?And that if I want the policies in the
App\Policies
namespace, do I need to use my ownguessPolicyName
?