2

In laravel 5.2 I want to route all the undefined url to one particular controller.

I am developing CMS like functionality, in which I want this thing.

Route::get('profile', 'Controller@profile');
Route::get('{any}', 'Controller@page');

so url like

www.domain.com/post/po-t/some/thing

www.domain.com/profile

so first url should redirect to page function and second url to profile function

Basically I want some idea for N-number or parameters, as in page it can be any number of parameters like "www.domain.com/post/po-t/some/thing"

Jay
  • 821
  • 2
  • 18
  • 50

3 Answers3

2

The route

Route::get('{any}', 'Controller@page');

only work fo a url like

www.domain.com/post

if you want it with more options you have to make another route like

Route::get('{any}/{any1}', 'Controller@page');

This will work for two options like this callback

www.domain.com/post/asdfgd

Zenel Rrushi
  • 2,346
  • 1
  • 18
  • 34
  • I know that, but I want solution for www.domain.com/post/asdfgd as we don't know what can be number or parameters – Jay Oct 13 '16 at 14:10
  • maybe you should try optional parameters. As far as i know there isn't a solution for what you want. The optional parameters come close to solution. `Route::get('{a?}/{b?}/{c?}/{d?}/{e?}/{f?}', 'Controller@page');` – Zenel Rrushi Oct 14 '16 at 09:53
0

Undefined route generate 404 HTTP status. You can create a 404.blade.php page on resources/views/errors, put whatever views you want to display. Whenever a 404 error occurred it will redirect you to that page. You don't need to do anything else, laravel will take care of the rest behind the scene.

Mahfuzul Alam
  • 3,057
  • 14
  • 15
0

Use middleware.

In the handle method you have access to the $request object. When the route isn't found redirect to your fallback route. See this question for the options for fetching the current url

Edit: An implementation can be found in the Laracasts forum. The poster wants to guard admin routes:

public function handle($request, Closure $next)
{
    $routeName = Route::currentRouteName();

    // isAdminName would be a quick check. For example,
    // you can check if the string starts with 'admin.'
    if ($this->isAdminName($routeName))
    {
        // If so, he's already accessing an admin path,
        // Just send him on his merry way.
        return $next($request);
    }

    // Otherwise, get the admin route name based on the current route name.
    $adminRouteName = 'admin.' . $routeName;

    // If that route exists, redirect him there.
    if (Route::has($adminRouteName))
    {
        return redirect()->route($adminRouteName);
    }

    // Otherwise, redirect him to the admin home page.
    return redirect('/admin');
}
Community
  • 1
  • 1
winkbrace
  • 2,682
  • 26
  • 19