I have two url's one for category and one for brand such as:
http://localhost/project/womens-fashion #category
http://localhost/project/babette-clothes #brand
I just wanted to make one route but call different controller. I have written the route but its not work for me its send error. See below code:
<?php
use \DB;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\Redirect;
Route::get('/','HomeController@index');
Route::get('/product', array('uses' => 'ProductController@index'));
Route::get('/{slug}', function($slug) {
$result = DB::select('SELECT controller FROM url_setting where slug = ?', [$slug]);
if ($result[0]->pw_us_controller == 'CategoryController@view') {
return Redirect::action('CategoryController@view', array($slug));
} elseif ($result[0]->pw_us_controller == 'CategoryController@view') {
return Redirect::action('BrandController@index', array($slug));
} else {
return Redirect::action('HomeController@index');
}
});
Error: InvalidArgumentException in UrlGenerator.php line 576: Action App\Http\Controllers\CategoryController@view not defined.
I am pretty confused, what went wrong? any idea!!!
You should define the route for CategoryController@view.
Try adding something like this in your route file:
Route::get('/category', 'CategoryController@view');
---EDIT---
I just read better the question. I think you would to obtain something like this:
/womens-fashion --> CategoryController@view
/babette-clothes --> BrandController@view
and you have slugs stored in your DB.
So, perhaps redirect is not your solution.
I would do something like this:
Route::get('/{slug}', 'SlugController@view');
controller SlugController:
class SlugController extends Controller
{
public function view(Request $request, $slug)
{
$result = DB::select('SELECT controller FROM url_setting where slug = ?', [$slug]);
if ($result[0]->pw_us_controller == 'CategoryController@view') {
return self::category($request, $slug);
} else if ($result[0]->pw_us_controller == 'BrandController@view') {
return self::brand($request, $slug);
} else {
// redirect to home
}
}
private function category($request, $slug)
{
// Category controller function
// ....
}
private function brand($request, $slug)
{
// Brand controller function
// ....
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With