用于搜索的 Laravel 路由和控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27641911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:35:29  来源:igfitidea点击:

Laravel Routing and Controller for Search

phplaravel

提问by Moose

I'm building my first basic laravel web app, after following a few tutorials this is the first one I'm tinkering with on my own. I'm running into to some trouble with routing to a controller and then getting the correct url.

我正在构建我的第一个基本的 Laravel Web 应用程序,在学习了一些教程之后,这是我自己修补的第一个应用程序。我在路由到控制器然后获取正确的 url 时遇到了一些麻烦。

Ideally at this point I should only have two routes /and /{user}. On the homepage you can search via a form for the user and the form should take you to /{user}.

理想情况下,此时我应该只有两条路线//{user}. 在主页上,您可以通过用户的表单进行搜索,该表单应将您带到/{user}

Routes (I have three cause I'm still trying to get this to work, and I think I need a POST):

路线(我有三个原因我仍在努力让它工作,我想我需要一个 POST):

Route::get('/', 'HomeController@index');
Route::get('/{user}', 'HomeController@student');
Route::post('/', 'HomeController@studentLookUp');

Home Controller:

家庭控制器:

public function index()
{
    return View::make('helpdesk');
}

public function student($user) {
    return View::make('selfservice')
        ->with('user', $user);
}

public function studentLookUp() {
    $user = Input::get('ID');

    return View::make('selfservice')
        ->with('user', $user);
}

Form:

形式:

{{ Form::open(array('class'=>'navbar-form navbar-left', 'role'=>'search'), array('action' => 'HomeController@student')) }}

  <div class="form-group">
    {{ Form::text('ID', '', array('placeholder'=>'ID', 'class'=>'form-control') ); }}
  </div>

  {{ Form::button('Search', array('class'=>'btn btn-default')) }}  

{{ Form::close() }}

At this point I can search from the homepage ('/') and it will take me back to the homepage but with the searched for user which is how I want it to work except it doesn't have the right url of homepage.com/username.

在这一点上,我可以从主页 ('/') 进行搜索,它会将我带回主页,但是搜索到的用户是我希望它工作的方式,除了它没有正确的homepage.com/username.

Any help would be much appreciated!

任何帮助将非常感激!

回答by Bit_hunter

First register a route to listen your search request:

首先注册一个路由来监听你的搜索请求:

1. Search Route:Register search route.

1. 搜索路线:注册搜索路线。

//route search 
Route::get('/search',['uses' => 'SearchController@getSearch','as' => 'search']);

2. Search View:-Now create a search form in a view:-

2. 搜索视图:-现在在视图中创建一个搜索表单:-

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

3. SearchController :

3. 搜索控制器:

Now create SearchController to handle your searching logic. SearchController :

现在创建 SearchController 来处理您的搜索逻辑。搜索控制器:

    <?php

    class SearchController extends \BaseController {


        public function getSearch()
        {
            //get keywords input for search
            $keyword=  Input::get('q');

            //search that student in Database
             $students= Student::find($keyword);

            //return display search result to user by using a view
            return View::make('selfservice')->with('student', $students);
        }

    }

Now you have to create one view selfserviceto display your search result.

现在您必须创建一个视图自助服务来显示您的搜索结果。

4. Selfservice View:

4.自助服务视图:

@foreach ($students as $key=> $student)
<div>
<a href="{{ URL::route('student.show', ['id' => $student->id]) }}">{{$student->name}}</a>
</div>              
@endforeach

Here for each student result, one link will be created. That link will be link:-

此处将为每个学生结果创建一个链接。该链接将是链接:-

website.domain/{student}

5. Update Routes for Student

5. 更新学生路线

Route::get('/{student}',['uses' => 'HomeController@student','as' => 'student.show']);


UPDATEupdated the answer to get student page directly

UPDATE更新答案直接获取学生页面



To redirect from search to website.domain\{user}follow these steps:-

要从搜索重定向以执行website.domain\{user}以下步骤:-

1. Modify SearchController

1.修改SearchController

<?php

class SearchController extends \BaseController {


    public function getSearch()
    {
        //get keywords input for search
        $keyword=  Input::get('q');

        //search that student in Database
         $student= Student::find($keyword);

        //redirect directly to student.show route with student detail
        return Redirect::route('student.show', array('student' => $student));
    }

}

2.Now add a function for Route student.showin HomeController Route::get('/{student}',['uses' => 'HomeController@student','as' => 'student.show']);

2.现在student.show在 HomeController 中 为 Route 添加一个函数Route::get('/{student}',['uses' => 'HomeController@student','as' => 'student.show']);

In HomeController

在家庭控制器中

public function student($student)
{
    //here display student detail
}