Skip to content
Permalink
Browse files

allow registration of guesser callback'

  • Loading branch information...
taylorotwell committed Feb 18, 2019
1 parent dee225f commit 150a63d491c89f73a8153c4548406e776e5f150a
Showing with 24 additions and 0 deletions.
  1. +24 −0 src/Illuminate/Auth/Access/Gate.php
@@ -64,6 +64,13 @@ class Gate implements GateContract
*/
protected $stringCallbacks = [];
/**
* The callback to be used to guess policy names.
*
* @var callable|null
*/
protected $guessPolicyNamesUsingCallback;
/**
* Create a new gate instance.
*
@@ -550,11 +557,28 @@ public function getPolicyFor($class)
*/
protected function guessPolicyName($class)
{
if ($this->guessPolicyNamesUsingCallback) {
return call_user_func($this->guessPolicyNamesUsingCallback, $class);
}
$classDirname = str_replace('/', '\\', dirname(str_replace('\\', '/', $class)));
return $classDirname.'\\Policies\\'.class_basename($class).'Policy';
}

This comment has been minimized.

@troccoli

troccoli Feb 20, 2019

Contributor

If my models are in the App\Models namespace, does it mean I need to put the policies in the App\Models\Policies namespace?

And that if I want the policies in the App\Policies namespace, do I need to use my own guessPolicyName?

This comment has been minimized.

@simonschaufi

simonschaufi Feb 23, 2019

I also put my Models in a Models folder which just makes the root folder a lot cleaner. I would also like to know how to do the auto mapping magic @taylorotwell

This comment has been minimized.

@andrewgorpenko

andrewgorpenko Mar 8, 2019

Ya, that would be a nice option

/**
* 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.
*

2 comments on commit 150a63d

@devcircus

This comment has been minimized.

Copy link
Contributor

devcircus replied Feb 23, 2019

Just look at the last few lines of the guessPolicyName method for inspiration then assign your own callback to `guessPolicyNamesUsingCallback```.

@ludo237

This comment has been minimized.

Copy link

ludo237 replied Mar 8, 2019

An example would be nice for this.

Please sign in to comment.
You can’t perform that action at this time.