Support the ongoing development of Laravel.io →
posted 10 years ago
Requests
Last updated 2 years ago.
0

One simple change - add $slug to the anonymous function parameters:

Route::get('/{slug}', function($slug) {
        if (is_numeric($slug)) {
            //call UserController@index
        } else {
            //call HomeController@index
        }
});
Last updated 2 years ago.
0

Ah yes, a bug in my example. But that's not the problem I was having.

The problem is "//call UserController@index" part, how do I call that from a route?

Last updated 2 years ago.
0

Hello,

create two different routes with filtering... something like:

Route::get( '/{slug}', 'UserController@Index')->where(array('slug' => '[0-9]+'));
Route::get( '/{slug}', 'HomeController@Index');
Last updated 2 years ago.
0

Sorry, the example I gave was really bad. The data type of the slug doesn't matter.

What I intend to do is to check the database with the $slug to see what category of data it is before sending the user to the correct controller.

Last updated 2 years ago.
0

It seems to me you are trying to do too much in the controller unless you just want to check the database and then redirect to a new route.

Route::get('/{slug}', function($slug) {
    // Figure out category of data
    // $category = DB::select(...)->first();

    if ($category == 'user') {
        return Redirect::action('UserController@index', array($slug));
    } elseif ($category == 'post') {
        return Redirect::action('PostController@index', array($slug));
    } else {
        return Redirect::action('HomeController@index');
    }
});

If you want to process different categories of data on the same request, you should probably be calling service classes that can handle that particular category of data. It's a bit hard to be specific without more information.

Last updated 2 years ago.
0

Previously I had separate routes for domain.com/u/<user> and domain.com/<page_slug> But my boss wants to use the same domain.com/<user_or_page_slug> now.

So to make this change with as little code affected as possible, I decide that we will have a single route that calls 2 different controllers based on what the user requested.

But I can't use a redirect because that would change the URL

Last updated 2 years ago.
0

Something like this should work for you:

Route::get('/', 'HomeController@index');
Route::get('/about', 'HomeController@about');
// List all HomeController pages here

Route::get('/{slug}', function($slug) {
    // See if we have a user
    $user = User::where('slug', $slug)->first();

    if ($user) {
        return View::make('profile', compact('user'));
    } else {
        App::abort(404);
    }
});

If you put your page routes first, they will catch the valid pages. The rest will fall through as slugs. You look those slugs up in the DB and if they existing just display the view. Otherwise, show a 404 page.

Last updated 2 years ago.
0
Solution

After spending hours experimenting, this seems to be working for us.

Route::get('/{slug}', function($slug) {
    $type = Check DB to find out type of data

    if ($type == 'user') {
        $controller = new UserController;
        return $controller->view($slug);
    } else {
        $controller = new PageController;
        return $controller->view($slug);
    }
});
Last updated 2 years ago.
0

Isn't this simple :

$segment = Request::segment(1);

$type = SomeHelperClass::getTypeFromDB($segment);

if ( $type == 'user' ) {
	Route::get('/{username}', 'UserController@view');
} else {
	Route::get('/{slug}', 'PageController@view');
}
Last updated 2 years ago.
0

I never thought of that, I suppose that works too : )

Last updated 2 years ago.
0

You could use the Request::is () method from the request object to evaluate the URL pattern with a value. Is the value is found in your URL you can choose any option.

You could use any other option that commented here.

Last updated 2 years ago.
0

I had a similar issue and fixed it with this:

Route::get('/slug/', ['uses' => (isset($_GET['get_param'])?'MyController@getStuff':'MyController@getOtherStuff')]);

Hopefully there is a cleaner way of doing this but this means I can redirect to a specific method on a controller based on the get variables passed.

url.com/slug/?get_param=true

0

Sign in to participate in this thread!

Eventy

Your banner here too?

wyred wyred Joined 5 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.