相同的路由,但在 Laravel 5.1 路由中调用不同的控制器
Same route but call different controller in Laravel 5.1 routing
我有两个 url,一个用于类别,一个用于品牌,例如:
http://localhost/project/womens-fashion #category
http://localhost/project/babette-clothes #brand
我只想走一条路线,但调用了不同的控制器。
我已经写了路线,但它对我不起作用它的发送错误。见以下代码:
<?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');
}
});
错误:InvalidArgumentException in UrlGenerator.php line 576: Action App\Http\Controllers\CategoryController@view not defined.
我很困惑,哪里出了问题?有什么想法!!!
您更愿意使用这种语法:
return redirect()->action('CategoryController@view', array($slug));
您应该为 CategoryController@view
定义路线。
尝试在您的路由文件中添加类似这样的内容:
Route::get('/category', 'CategoryController@view');
---编辑---
我只是更好地阅读了这个问题。我想你会得到这样的东西:
/womens-fashion --> CategoryController@view
/babette-clothes --> BrandController@view
并且您的数据库中存储了 slug。
所以,也许 redirect 不是您的解决方案。
我会这样做:
Route::get('/{slug}', 'SlugController@view');
控制器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
// ....
}
}
我有两个 url,一个用于类别,一个用于品牌,例如:
http://localhost/project/womens-fashion #category
http://localhost/project/babette-clothes #brand
我只想走一条路线,但调用了不同的控制器。 我已经写了路线,但它对我不起作用它的发送错误。见以下代码:
<?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');
}
});
错误:InvalidArgumentException in UrlGenerator.php line 576: Action App\Http\Controllers\CategoryController@view not defined.
我很困惑,哪里出了问题?有什么想法!!!
您更愿意使用这种语法:
return redirect()->action('CategoryController@view', array($slug));
您应该为 CategoryController@view
定义路线。
尝试在您的路由文件中添加类似这样的内容:
Route::get('/category', 'CategoryController@view');
---编辑---
我只是更好地阅读了这个问题。我想你会得到这样的东西:
/womens-fashion --> CategoryController@view
/babette-clothes --> BrandController@view
并且您的数据库中存储了 slug。
所以,也许 redirect 不是您的解决方案。
我会这样做:
Route::get('/{slug}', 'SlugController@view');
控制器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
// ....
}
}