6

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
  • You mean Route::get('/{user}', 'HomeController@student'); doesn't work? Commented Dec 24, 2014 at 20:39

1 Answer 1

10

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
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, this is definitely helpful. At this point I can search for a user and they will show up, but the url is homepage.com/search. Any way to redirect to homepage.com/username after getting the query from the getSearch controller?
when search result be displayed,that time URL will be website.domain/search?q=keyword But in this search_result_view you will display all users and the URL of those users will be website.domain/{user}
first understand the flow Homepage=>search=>search_result=>user_page. There will be one intermediate view search_result between search and user view.After clicking on link available in sear_result view,page will be redirected to website.domain/{user}
That's what I thought. Anyway to be automatically routed without having to click the link on the search results page?
Thanks so much for your help. That did it and I learned a few new tricks in Laravel I didn't know about!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.