0

I am wanting to have a catch all route for catching any page that's isn't loaded. I have tried a couple of ways.

First way - I have created a seperate Service Provider and loaded it last in the config.

Second attempt was to intercept 404's

render method in Handler.php

if($exception instanceof NotFoundHttpException)
    return PageController::handleMissingPage();

Then the handleMissingPage() method

$slug = '/';

if(strlen($path) > 1)
    $slug = ltrim($path, '/');

if ($page = Page::where('path', $slug)->first())
    return response()
        ->view($page->template, ['pageBuilder' => $page], 200);

abort(404);

This second way works, but it doesn't feel correct intercepting 404's. The user is also never seen as logged in doing it this way

I'm stuck as to what to try next, I assumed loading it in last would give it last priority

Ian
  • 3,539
  • 4
  • 27
  • 48
  • https://stackoverflow.com/questions/21552604/how-to-define-a-laravel-route-with-a-parameter-that-contains-a-slash-character might help – tkausl Jun 24 '17 at 11:19
  • @tkausl I have already tried adding the route at the bottom and dumping it. – Ian Jun 24 '17 at 11:46
  • I'm assuming `Page::->where` is a typo? As it is, this will trigger a 500. You are fetching a Page by its slug? So the route to handle pages is already there? My guess is that you have a PageController and a route with a catch all already in place, that would be the location to handle your missing pages? – Robert Jun 24 '17 at 12:35
  • @Robert Indeed that is a typo in the question, is was previously `with()` but tried to keep the question tidy. I've been playing around with the app.php and loading in routes through service providers. It seems that app.php is loaded THEN service provider routes get appended, however unless I hard code the service providers in app.php they go all out of order, the catch all ends up near the top of the list, therefore no routes after are being loaded. I've temp fixed it by hard coding, but still looking for a perm solution. – Ian Jun 24 '17 at 13:02
  • `Route::get({'all?'}, // whatevs)->name('fourohfour');` and then `redirect()->route('fourohfour');` in your controller or whatever will do it, no? – ggdx Jun 24 '17 at 13:07
  • @DanWhite The problem seems to be nailed down to the order that the routes are loaded after some investigating. – Ian Jun 24 '17 at 13:08
  • I might be missing something, but why do you use a service provider for adding 1 dynamic route and a page controller? – Robert Jun 24 '17 at 13:08
  • @Ian placing the route declaration right at the very end of routes/web.php will do, then as long as you have the name, you can redirect from wherever necessary by calling redirect on that route name. – ggdx Jun 24 '17 at 13:10
  • @Robert It was just one of the ways I have tried, as it comes down to the order that routes are loaded. Figured if it was in a service provider, then it would be loaded after all other provides. – Ian Jun 24 '17 at 13:10
  • @DanWhite other packages get loaded after that route which makes their routes not accessable. – Ian Jun 24 '17 at 13:12
  • @Ian That's why you place it as the very last one – ggdx Jun 24 '17 at 13:13
  • @DanWhite I have done, and it still doesn't get loaded last – Ian Jun 24 '17 at 13:15

0 Answers0