Skip to content

Commit

Permalink
Restrict fluent route attributes to known attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber committed Dec 5, 2016
1 parent f353d75 commit e4f8619
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Illuminate/Routing/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Routing;

use Closure;
use BadMethodCallException;

class RouteRegistrar
{
Expand All @@ -29,6 +30,15 @@ class RouteRegistrar
'get', 'post', 'put', 'patch', 'delete', 'options', 'any',
];

/**
* The attributes that can be set through this class.
*
* @var array
*/
protected $allowedAttributes = [
'as', 'domain', 'middleware', 'name', 'namespace', 'prefix',
];

/**
* The attributes that are aliased.
*
Expand Down Expand Up @@ -149,6 +159,10 @@ public function __call($method, $parameters)
return $this->registerRoute($method, ...$parameters);
}

return $this->attribute($method, $parameters[0]);
if (in_array($method, $this->allowedAttributes)) {
return $this->attribute($method, $parameters[0]);
}

throw new BadMethodCallException("Method [{$method}] does not exist.");
}
}
20 changes: 20 additions & 0 deletions tests/Routing/RouteRegistrarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ public function testCanRegisterGroupWithDomain()
$this->assertEquals('{account}.myapp.com', $this->getRoute()->domain());
}

public function testCanRegisterGroupWithDomainAndNamePrefix()
{
$this->router->domain('{account}.myapp.com')->name('api.')->group(function ($router) {
$router->get('users', 'UsersController@index')->name('users');
});

$this->assertEquals('{account}.myapp.com', $this->getRoute()->domain());
$this->assertEquals('api.users', $this->getRoute()->getName());
}

/**
* @expectedException \BadMethodCallException
*/
public function testRegisteringNonApprovedAttributesThrows()
{
$this->router->domain('foo')->missing('bar')->group(function ($router) {
//
});
}

public function testCanRegisterResource()
{
$this->router->middleware('resource-middleware')
Expand Down

0 comments on commit e4f8619

Please sign in to comment.