来自搜索控制器的Laravel 5自定义路由

时间:2016-01-02 10:16:33

标签: php laravel custom-routes

我对Laravel很新,并且确实在Laracast观看免费视频,尽可能地尝试。我有一个简单的搜索输入,它带有一个邮政编码(例如:92804)。它搜索数据库并找到邮政编码,在行中查找“提供者”,然后根据找到的提供者返回一个视图(此时有4个提供者)。例如:92804-> anaheim-> ca-> provider1因此它将返回provider1的视图,其中包含所需的数据(城市和州)。我已经完成了这一点,但是当视图返回时它有

的URL
www.domain.com/search?q=92804.

如何制作显示的路线或路线。

www.domain.com/{city}-{state}-{provider1}

那些将是变量,因此在示例中它将是

www.domain.com/anaheim-ca-provider1

我在这里搜索并发现了这个:(但我相信这是旧版本的Laravel)Laravel Routing and Controller for Search

我的路线:

Route::get('/', 'PagesController@index');
Route::get('search',['uses' => 'SearchController@getSearch','as' => 'search']);
Route::get('/{city}-{state}-{zipcode}',['uses' => 'PagesController@provider1','as' => 'search.provider1']);
Route::get('/{city}-{state}-{zipcode}',['uses' => 'PagesController@provider2','as' => 'search.provider2']);
Route::get('/{city}-{state}-{zipcode}',['uses' => 'PagesController@provider3','as' => 'search.provider3']);
Route::get('/{city}-{state}-{zipcode}',['uses' => 'PagesController@provider4','as' => 'search.provider4'])

我在欢迎视图中的表单:

<form  action="search" method="get">
    <input type="text"  name="q" placeholder="Search.."/>
    <button type="submit">Search</button>
</form>

PagesController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class PagesController extends Controller
{
   public function index()
    {

        return view('welcome');

    }
public function provider1()
        {

            return view('providers/provider1/provider1Search');

        }
public function provider2()
            {

                return view('providers/provider2/provider2Search');

            }
public function provider3()
            {

                return view('providers/provider3/provider4Search');

            }
public function provider4()
            {

                return view('providers/provider4/provider4Search');

            }

}

SearchController:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class SearchController extends Controller
{
     public function getSearch(Request $request) 
        {
            $keyword =  $request->input('q');

            $data = DB::select("SELECT * FROM `TABLE 13` WHERE `Zip Code` = '$keyword'");

        $city = strtolower($data[0]->City);
        $state = strtolower($data[0]->State);
        $provider = strtolower($data[0]->{'Provider Name'});
        $zipcode = $data[0]->{'Zip Code'};

            if ($data[0]->{'Provider Name'})
                {
                    switch ($data[0]->{'Provider Name'})
                        {
                            case "provider1":
                                return redirect()->route('search.provider1',[$city,$state,$zipcode])->with('data',$data);
                                break;
                            case "provider2":
                                return return redirect()->route('search.provider2',[$city,$state,$zipcode])->with('data',$data);
                                break;
                            case "provider3":
                                return return redirect()->route('search.provider3',[$city,$state,$zipcode])->with('data',$data);
                                break;
                            case "provider4":
                                return return redirect()->route('search.provider4',[$city,$state,$zipcode])->with('data',$data);
                                break;
                        }
                }   
        }
}

我的观点:

  1. provider1Search
  2. provider2Search
  3. provider3Search
  4. provider4Search
  5. 欢迎
  6. 在providerSearch的视图中,我将城市,州,提供商称为:

    {{$data[0]->City}},{{$data[0]->State}},{{$data[0]->{'Provider Name'}}
    

    这些是我想在自定义路线中使用的变量

    示例:

    1. 邮政编码= 85257 = www.domain.com/scottsdale-az-provider1
    2. 邮政编码= 92804 = www.domain.com/anaheim-ca-provider2
    3. 邮政编码= 60605 = www.domain.com/chicago-il-provider3
    4. 邮政编码= 01527 = www.domain.com/millbury-ma-provider4

0 个答案:

没有答案