Laravel路由和搜索控制器

Laravel路由和搜索控制器

问题描述:

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

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.

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

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

路由(我有三个原因,我仍在尝试使其正常运行,我认为我需要一个POST):

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');

家庭控制器:

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::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() }}

这时我可以从主页('/')中搜索,它将带我回到主页,但是搜索到的用户正是我希望它工作的方式,但它没有正确的URL. homepage.com/username .

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.

任何帮助将不胜感激!

首先注册一条路由以侦听您的搜索请求:

First register a route to listen your search request:

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

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

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

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:

现在创建SearchController来处理您的搜索逻辑.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.自助服务视图:

@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.为学生更新路线

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


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


UPDATE updated the answer to get student page directly

要将搜索从重定向到 website.domain \ {user} ,请按照以下步骤操作:-

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

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..现在在HomeController中为Route student.show 添加一个功能. Route :: get('/{student}',['uses'=>'HomeController @ student','as'=>'student.show']);

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

在HomeController中

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