Skip to content

Commit

Permalink
Fix 404 error when URLs have both ending slash and query parameters (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Apr 8, 2021
1 parent c42e09a commit 9cac7f3
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Http/Controllers/FrontendController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ public function __construct()
*/
public function index(Request $request)
{
$url = Site::current()->relativePath(
str_finish($request->getUri(), '/')
);

if ($url === '') {
$url = '/';
}
$url = Site::current()->relativePath($request->getUri());

if (Statamic::isAmpRequest()) {
$url = str_after($url, '/'.config('statamic.amp.route'));
Expand All @@ -45,6 +39,10 @@ public function index(Request $request)
$url = substr($url, 0, strpos($url, '?'));
}

if (Str::endsWith($url, '/') && Str::length($url) > 1) {
$url = rtrim($url, '/');
}

if ($data = Data::findByUri($url, Site::current()->handle())) {
return $data;
}
Expand Down
117 changes: 117 additions & 0 deletions tests/FrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,123 @@ public function page_is_displayed_with_query_string()
$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function page_is_displayed_with_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');

$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);

$response = $this->get('/about/')->assertStatus(200);

$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function page_is_displayed_with_query_string_and_ending_slash()
{
$this->withStandardBlueprints();
$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '{{ template_content }}');
$this->viewShouldReturnRaw('some_template', '<h1>{{ title }}</h1> <p>{{ content }}</p>');

$page = $this->createPage('about', [
'with' => [
'title' => 'The About Page',
'content' => 'This is the about page.',
'template' => 'some_template',
],
]);

$response = $this->get('/about/?some=querystring')->assertStatus(200);

$this->assertEquals('<h1>The About Page</h1> <p>This is the about page.</p>', trim($response->content()));
}

/** @test */
public function home_page_on_second_subdirectory_based_site_is_displayed()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('/fr')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_subdirectory_based_site_is_displayed_with_ending_slash()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('/fr/')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_domain_site_is_displayed()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('http://anotherhost.com')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

/** @test */
public function home_page_on_second_domain_site_is_displayed_with_ending_slash()
{
Site::setConfig(['sites' => [
'english' => ['url' => 'http://localhost/', 'locale' => 'en'],
'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'],
]]);

$this->createHomePagesForTwoSites();

$response = $this->get('http://anotherhost.com/')->assertStatus(200);

$this->assertEquals('French Home', trim($response->content()));
}

private function createHomePagesForTwoSites()
{
$this->withStandardBlueprints();
$this->withoutExceptionHandling();
$this->withStandardFakeViews();

$c = tap(Collection::make('pages')->sites(['english', 'french'])->routes('{slug}')->structureContents(['root' => true]))->save();

EntryFactory::id('1')->locale('english')->slug('home')->collection('pages')->data(['content' => 'Home'])->create();
EntryFactory::id('2')->locale('french')->slug('french-home')->collection('pages')->data(['content' => 'French Home'])->create();

$c->structure()->in('english')->tree([['entry' => '1']])->save();
$c->structure()->in('french')->tree([['entry' => '2']])->save();
}

/** @test */
public function drafts_are_not_visible()
{
Expand Down

0 comments on commit 9cac7f3

Please sign in to comment.