我有两个网址,一个是类别,另一个是品牌,例如:
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.
我很困惑,出什么问题了?知道吗!
发布于 2015-11-30 03:16:41
您应该定义CategoryController@view
的路由。
尝试在您的路由文件中添加这样的内容:
Route::get('/category', 'CategoryController@view');
--编辑--
我只是把问题读得更清楚了。我想你会得到这样的东西:
/womens-fashion --> CategoryController@view
/babette-clothes --> BrandController@view
你的数据库里有子弹。
因此,也许重定向不是您的解决方案。
我会这样做:
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
// ....
}
}
发布于 2015-11-30 02:56:14
您更愿意使用以下语法:
return redirect()->action('CategoryController@view', array($slug));
https://stackoverflow.com/questions/33996818
复制相似问题