Skip to content

Commit

Permalink
[1.x] Fixes index views on nested model route bindings (#108)
Browse files Browse the repository at this point in the history
* Fixes index views on nested model route bindings

* Updates test
  • Loading branch information
nunomaduro authored Sep 8, 2023
1 parent 0e353c3 commit 765b765
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/Pipeline/MatchWildcardDirectories.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ class MatchWildcardDirectories
*/
public function __invoke(State $state, Closure $next): mixed
{
if (! $state->onLastUriSegment() &&
$directory = $this->findWildcardDirectory($state->currentDirectory())) {
return new ContinueIterating($state->withData(
if ($directory = $this->findWildcardDirectory($state->currentDirectory())) {
$state = $state->withData(
Str::of($directory)
->basename()
->match('/\[(.*)\]/')->value(),
$state->currentUriSegment(),
)->replaceCurrentUriSegmentWith(
Str::of($directory)->basename()
));
);

if (! $state->onLastUriSegment()) {
return new ContinueIterating($state);
}

if (file_exists($path = $state->currentUriSegmentDirectory().'/index.blade.php')) {
return new MatchedView($path, $state->data);
}
}

return $next($state);
Expand Down
9 changes: 6 additions & 3 deletions tests/Feature/Console/ListCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@
GET /podcasts/{podcast}/comments/{comment:id} podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tests.Feature.Fixtures.Comment-id].blad…
GET /posts/{lowerCase}/{upperCase}/{podcast}/{user:email}/show posts.show › posts/[lowerCase]/[UpperCase]/[Podcast]/[User-email]/show.bla…
GET /users/articles/{user:wrongColumn} ........................................ user.articles › users/articles/[User-wrong_column].blade.php
GET /users/movies/{user}/{movie} ............... users/movies/[.Tests.Feature.Fixtures.User]/[.Tests.Feature.Fixtures.Movie]/index.blade.php
GET /users/nuno .......................................................................................... users.nuno › users/nuno.blade.php
GET /users/{id} .......................................................................................... users.show › users/[id].blade.php
Showing [17] routes
Showing [18] routes


EOF);
Expand Down Expand Up @@ -128,10 +129,11 @@
GET /non-routables/{nonRoutable} ............................................. non-routables/[.Tests.Feature.Fixtures.NonRoutable].blade.php
GET /posts/{lowerCase}/{upperCase}/{podcast}/{user:email}/show posts.show › posts/[lowerCase]/[UpperCase]/[Podcast]/[User-email]/show.bla…
GET /users/articles/{user:wrongColumn} ........................................ user.articles › users/articles/[User-wrong_column].blade.php
GET /users/movies/{user}/{movie} ............... users/movies/[.Tests.Feature.Fixtures.User]/[.Tests.Feature.Fixtures.Movie]/index.blade.php
GET /users/nuno .......................................................................................... users.nuno › users/nuno.blade.php
GET /users/{id} .......................................................................................... users.show › users/[id].blade.php
Showing [12] routes
Showing [13] routes


EOF);
Expand Down Expand Up @@ -230,12 +232,13 @@
GET /podcasts/{podcast}/comments/{comment:id} tests/Feature/resources/views/pages/podcasts/[.Tests.Feature.Fixtures.Podcast]/comments/[.Tes…
GET /posts/{lowerCase}/{upperCase}/{podcast}/{user:email}/show posts.show › tests/Feature/resources/views/pages/posts/[lowerCase]/[UpperC…
GET /users/articles/{user:wrongColumn} .... user.articles › tests/Feature/resources/views/pages/users/articles/[User-wrong_column].blade.php
GET /users/movies/{user}/{movie} tests/Feature/resources/views/pages/users/movies/[.Tests.Feature.Fixtures.User]/[.Tests.Feature.Fixtures.M…
GET /users/nuno ...................................................... users.nuno › tests/Feature/resources/views/pages/users/nuno.blade.php
GET /users/{id} ...................................................... users.show › tests/Feature/resources/views/pages/users/[id].blade.php
GET /{...user} ................................................................ tests/Feature/resources/views/more-pages/[...User].blade.php
GET /{...user}/detail ......................... more-pages.user.detail › tests/Feature/resources/views/more-pages/[...User]/detail.blade.php
Showing [20] routes
Showing [21] routes


EOF);
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/Fixtures/Movie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Feature\Fixtures;

use Illuminate\Database\Eloquent\Model;

class Movie extends Model
{
protected $table = 'movies';

protected $guarded = [];

public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
}
5 changes: 5 additions & 0 deletions tests/Feature/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class User extends BaseUser
{
protected $guarded = [];

public function movies(): HasMany
{
return $this->hasMany(Movie::class);
}

public function books(): HasMany
{
return $this->hasMany(Book::class);
Expand Down
34 changes: 34 additions & 0 deletions tests/Feature/ModelBindingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,23 @@
use Laravel\Folio\Pipeline\PotentiallyBindablePathSegment;
use Tests\Feature\Fixtures\Event;
use Tests\Feature\Fixtures\Podcast;
use Tests\Feature\Fixtures\User;

beforeEach(function () {
Schema::create('users', function ($table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});

Schema::create('movies', function ($table) {
$table->id();
$table->foreignId('user_id');
$table->string('name');
$table->timestamps();
});

Schema::create('podcasts', function ($table) {
$table->id();
$table->string('name');
Expand Down Expand Up @@ -79,6 +94,25 @@
$this->get('/podcasts/missing-id')->assertNotFound();
});

test('child implicit bindings are resolved', function () {
$user = User::create([
'name' => 'test-name',
'email' => '[email protected]',
]);

$user->movies()->create([
'name' => 'test-movie-name',
]);

$movie = $user->movies()->create([
'name' => 'test-movie-name-2',
]);

$this->get('/users/movies/'.$user->id.'/'.$movie->id)->assertSee(
'test-name: test-movie-name-2.',
);
});

test('child implicit bindings are scoped to the parent if field is present', function () {
$podcast = Podcast::create([
'name' => 'test-podcast-name',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
{{ $user->name }}: {{ $movie->name }}.
</div>

0 comments on commit 765b765

Please sign in to comment.