duanhongqiong9460 2014-12-24 20:31
浏览 61
已采纳

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

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.

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}.

Routes (I have three cause I'm still trying to get this to work, and I think I need a 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.

Any help would be much appreciated!

  • 写回答

1条回答 默认 最新

  • drq22639 2014-12-24 20:45
    关注

    First register a route to listen your search request:

    1. Search Route: Register search route.

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

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

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

    3. SearchController :

    Now create SearchController to handle your searching logic. 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 selfservice to display your search result.

    4. Selfservice View:

    @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

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

    UPDATE updated the answer to get student page directly


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

    1. Modify 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.show in HomeController Route::get('/{student}',['uses' => 'HomeController@student','as' => 'student.show']);

    In HomeController

    public function student($student)
    {
        //here display student detail
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序