lundi 29 juin 2020

Laravel URL rewriting and routing

I am coding a webapp based on laravel. But getting some difficulties in routing function. Can you help me by providing suitable code.

Lets start with a example.

User is on index page http://example.com On this page there is a list of urls that is dynamically generated.

Every url has a format like - http://example.com/messages?n=79688

here 79688 is message id.

It want the url to be like http://example.com/messages/79688

Here is web.php //Route for Messages

Route::get('/messages', 'HomeController@messages')->name('Messages');

Here is HomeController.php

public function messages(Request $request) 
{ 
    if(isset($request->n)) { 
        $number = $request->input('n'); 
        $number_check = Number::select('type')->where('number', $number)->first(); 
        $per_page = env('PER_PAGE', 15); 
        $query = Message::where('number', $number); 
        $messages = $query->orderBy('created_at', 'DESC')->paginate($per_page); 
        $messages->withPath('?n='.$number); 
        return view('messages')->with('messages', $messages)->with('number', $number)->with('count', $messages->total());
    } else { 
        return redirect()->route('Main')->with('error', "Message not found"); 
    } 
}

Can you please tell me how to modify this controller so that it can catch message id from url http://example.com/messages/79688



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire