Laravel 而不是在 Controller 中使用 Session 和 web.php
Laravel instead using Session in Controller and web.php
我们有一个餐厅页面,用户可以在其中添加他的 zip,我们正在显示餐厅。我们已经这样解决了:
web.php
Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
Route::get('/', 'RestaurantController@showAllRestaurants');
Route::post('/', 'RestaurantController@showAllRestaurants');
Route::get('search','RestaurantController@searchRestaurant');
Route::post('typefilter','RestaurantController@productTypeFilter');
RestaurantController.php
public function showAllRestaurants(Request $request)
{
$getZipCode = session::get('zipcode',$request->zip_code);
if(!empty($getZipCode))
{
if(Auth::check()) {
$country_code = Auth::user()->country_code;
} else {
$country_code = Common::GetIPData()->iso_code;
}
// get all restaurant using zipcode
$all_restaurant = Restaurant::leftJoin('restaurant_delivery_areas','restaurant_delivery_areas.restaurant_id','=','restaurants.id')
->leftJoin('zip_codes','zip_codes.id','=','restaurant_delivery_areas.zip_code_id')
->leftJoin('restaurant_cuisines','restaurant_cuisines.restaurant_id','=','restaurants.id')
->where('restaurants.country_code',$country_code)
->where(function ($q) use($getZipCode) {
$q->where('restaurants.zip',$getZipCode)
->orWhere('zip_codes.postal_code',$getZipCode)
->where('restaurant_delivery_areas.is_active','=',1);
});
所以现在我们想为每个 zip 文件创建一个页面,例如:test.com/restaurants/zip
有人有什么建议吗?
不确定我是否理解你的问题。但在我看来,您只想将邮政编码作为 url 参数传递,而不是在 GET 查询中传递。
如果是这样,您可以像这样接收 zip 作为 showAllRestaurants() 方法的第二个参数:
public function showAllRestaurants(Request $request, $zip_code){
//...
}
现在,zip_code 被接收到您方法中的 $zip_code 变量。
并更改 web.php 以支持它。
Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants');
Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants');
Route::get('search','RestaurantController@searchRestaurant');
Route::post('typefilter','RestaurantController@productTypeFilter');
为了避免在这种路由情况下发生冲突,您应该使用一些正则表达式来告诉 laravel 什么是 zip_code
,否则如果您说 /restaurants/search,它会认为'search' 单词是 zip_code.
如果您的 zip_code 只有数字。您可以像下面这样在路由上添加 where() 子句。
Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
如果您的 zip_code 包含其他字符,您应该 google(或自己制作)一些适合您的 zip_code 格式的正则表达式。
希望这就是您想要的。
我们有一个餐厅页面,用户可以在其中添加他的 zip,我们正在显示餐厅。我们已经这样解决了: web.php
Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
Route::get('/', 'RestaurantController@showAllRestaurants');
Route::post('/', 'RestaurantController@showAllRestaurants');
Route::get('search','RestaurantController@searchRestaurant');
Route::post('typefilter','RestaurantController@productTypeFilter');
RestaurantController.php
public function showAllRestaurants(Request $request)
{
$getZipCode = session::get('zipcode',$request->zip_code);
if(!empty($getZipCode))
{
if(Auth::check()) {
$country_code = Auth::user()->country_code;
} else {
$country_code = Common::GetIPData()->iso_code;
}
// get all restaurant using zipcode
$all_restaurant = Restaurant::leftJoin('restaurant_delivery_areas','restaurant_delivery_areas.restaurant_id','=','restaurants.id')
->leftJoin('zip_codes','zip_codes.id','=','restaurant_delivery_areas.zip_code_id')
->leftJoin('restaurant_cuisines','restaurant_cuisines.restaurant_id','=','restaurants.id')
->where('restaurants.country_code',$country_code)
->where(function ($q) use($getZipCode) {
$q->where('restaurants.zip',$getZipCode)
->orWhere('zip_codes.postal_code',$getZipCode)
->where('restaurant_delivery_areas.is_active','=',1);
});
所以现在我们想为每个 zip 文件创建一个页面,例如:test.com/restaurants/zip
有人有什么建议吗?
不确定我是否理解你的问题。但在我看来,您只想将邮政编码作为 url 参数传递,而不是在 GET 查询中传递。
如果是这样,您可以像这样接收 zip 作为 showAllRestaurants() 方法的第二个参数:
public function showAllRestaurants(Request $request, $zip_code){
//...
}
现在,zip_code 被接收到您方法中的 $zip_code 变量。
并更改 web.php 以支持它。
Route::group(['prefix' => 'restaurants', 'namespace' => 'frontEnd', 'middleware'=>'checkzipcode'], function () {
Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants');
Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants');
Route::get('search','RestaurantController@searchRestaurant');
Route::post('typefilter','RestaurantController@productTypeFilter');
为了避免在这种路由情况下发生冲突,您应该使用一些正则表达式来告诉 laravel 什么是 zip_code
,否则如果您说 /restaurants/search,它会认为'search' 单词是 zip_code.
如果您的 zip_code 只有数字。您可以像下面这样在路由上添加 where() 子句。
Route::get('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
Route::post('/{zip_code}', 'RestaurantController@showAllRestaurants')->where('zip_code', '[0-9]+');
如果您的 zip_code 包含其他字符,您应该 google(或自己制作)一些适合您的 zip_code 格式的正则表达式。
希望这就是您想要的。