Skip to content

Commit

Permalink
Apply no_prefix update to 5.8 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
loonytoons committed Sep 24, 2019
1 parent 0a2baea commit 1dd8d93
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 5 deletions.
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,10 @@ Here's an example that uses all of the available parameters for a `@Get` annotat

```php
/**
* @Get("/profiles/{id}", as="profiles.show", middleware="guest", domain="foo.com", where={"id": "[0-9]+"})
* @Get("/profiles/{id}", as="profiles.show", middleware="guest", domain="foo.com", where={"id": "[0-9]+"}, no_prefix="true")
*/
```
`no_prefix` allows any prefix added to the routes in that controller be ignored for this particular route.

### @Post, @Options, @Put, @Patch, @Delete, @any

Expand All @@ -252,7 +253,7 @@ Or on a whole controller, with the same only/exclude filter syntax that you can

```php
/**
* @Middleware("guest", except={"logout"})
* @Middleware("guest", except={"logout"}, prefix="/your/prefix")
*/
class AuthController extends Controller {

Expand Down
4 changes: 3 additions & 1 deletion src/Routing/Annotations/Annotations/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function modifyCollection(EndpointCollection $endpoints, ReflectionClass
protected function prefixEndpoints(EndpointCollection $endpoints)
{
foreach ($endpoints->getAllPaths() as $path) {
$path->path = $this->trimPath($this->prefix, $path->path);
if (!$path->no_prefix) {
$path->path = $this->trimPath($this->prefix, $path->path);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Routing/Annotations/Annotations/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function modify(MethodEndpoint $endpoint, ReflectionMethod $method)
{
$endpoint->addPath(new Path(
strtolower(class_basename(get_class($this))), $this->domain, $this->value,
$this->as, (array) $this->middleware, (array) $this->where
$this->as, (array) $this->middleware, (array) $this->where, $this->no_prefix
));
}
}
10 changes: 9 additions & 1 deletion src/Routing/Annotations/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class Path extends AbstractPath
*/
public $as;

/**
* Should the prefix added to the controller be ignored for this particular route
*
* @var bool
*/
public $no_prefix;

/**
* Create a new Route Path instance.
*
Expand All @@ -23,13 +30,14 @@ class Path extends AbstractPath
*
* @return void
*/
public function __construct($verb, $domain, $path, $as, $middleware = [], $where = [])
public function __construct($verb, $domain, $path, $as, $middleware = [], $where = [], $no_prefix = false)
{
$this->as = $as;
$this->verb = $verb;
$this->where = $where;
$this->domain = $domain;
$this->middleware = $middleware;
$this->path = $path == '/' ? '/' : trim($path, '/');
$this->no_prefix = $no_prefix;
}
}
9 changes: 9 additions & 0 deletions tests/Routing/RoutingAnnotationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public function testWhereAnnotationDetail()
$this->assertEquals(include __DIR__ . '/results/route-detail-where.php', $routeDetail);
}

public function testPrefixAnnotation()
{
require_once __DIR__.'/fixtures/annotations/PrefixController.php';
$scanner = $this->makeScanner(['App\Http\Controllers\PrefixController']);

$definition = str_replace(PHP_EOL, "\n", $scanner->getRouteDefinitions());
$this->assertEquals(trim(file_get_contents(__DIR__.'/results/annotation-prefix.php')), $definition);
}

/**
* Construct a route annotation scanner.
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Routing/fixtures/annotations/PrefixController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

/**
* @Controller(prefix="/any-prefix")
*/
class PrefixController
{
/**
* @Get("route-with-prefix", as="route.with.prefix")
*/
public function withPrefixAnnotations()
{
}

/**
* @Get("route-without-prefix", as="route.without.prefix", no_prefix="true")
*/
public function withoutPrefixAnnotations()
{
}
}
15 changes: 15 additions & 0 deletions tests/Routing/results/annotation-prefix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$router->get('any-prefix/route-with-prefix', [
'uses' => 'App\Http\Controllers\PrefixController@withPrefixAnnotations',
'as' => 'route.with.prefix',
'middleware' => [],
'where' => [],
'domain' => NULL,
]);

$router->get('route-without-prefix', [
'uses' => 'App\Http\Controllers\PrefixController@withoutPrefixAnnotations',
'as' => 'route.without.prefix',
'middleware' => [],
'where' => [],
'domain' => NULL,
]);

0 comments on commit 1dd8d93

Please sign in to comment.