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
}
});
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?
Hello,
create two different routes with filtering... something like:
Route::get( '/{slug}', 'UserController@Index')->where(array('slug' => '[0-9]+'));
Route::get( '/{slug}', 'HomeController@Index');
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.
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.
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
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.
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);
}
});
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');
}
I never thought of that, I suppose that works too : )
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.
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community