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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来