0

I want my view page to open when the type the url as www.domain-name.in/sample/ID. ID can be an integer valure of 5 digits. Right now my view page is opening when I type the url as www.domain-name.in/sample but when i type www.domain-name.in/sample/ID, I'm getting 404 error. This is my route :

Route::get('/{$id}','HomeController@index');

This is my controller

public function index(Request $request,$id)
    {
        return view('filename');
    }

How can I get view page with this url : www.domain-name.in/sample/ID

Cleptus
  • 3,446
  • 4
  • 28
  • 34
  • Please show your route `web.php`. – Amit Senjaliya Feb 17 '20 at 05:29
  • Route::get('/{$id}','HomeController@index'); –  Feb 17 '20 at 05:34
  • Have you added route prefix then I have updated my answer try it – Amit Senjaliya Feb 17 '20 at 05:35
  • 1
    Please remove the `$` sign in your route. No need to use `$`. `Route::get('/{id}', 'HomeController@index');` – Amit Senjaliya Feb 17 '20 at 05:37
  • If I am not mistaken, any Laravel route that you defined gives a 404 here and if that is the case, see https://laracasts.com/discuss/channels/laravel/why-do-i-always-get-a-404-error-for-any-route-i-create and https://stackoverflow.com/questions/13514990/laravel-4-all-routes-except-home-result-in-404-error – nice_dev Feb 17 '20 at 06:26
  • Also, route such as `/{$id}` is bad as it could match any other route as well. You better put that under some route group which has a prefix or have some fixed URI at the start. – nice_dev Feb 17 '20 at 06:27

2 Answers2

0

Your route will be

Route::get('sample/{id}','HomeController@index')->where(['id' => '[0-9]{1,5}');

Your controller will be

public function index($id)
{
   echo $id;
   exit;
   return view('filename');
}

Now you can access it as url as below.

www.domain-name.in/sample/12345

"12345" is integer value of 5 digits sample id.

Foued MOUSSI
  • 4,643
  • 3
  • 19
  • 39
Dilip Hirapara
  • 14,810
  • 3
  • 27
  • 49
0

You need to use the below code:

If you defined route group in web.php then use:

Route::group(['prefix' => 'sample'], function(){
    Route::get('/{id}', 'HomeController@index')->name('home');
});

And in the controller:

public function index(Request $request,$id)
{
    $myid = $id;
    return view('filename', compact('myid'));
}
Amit Senjaliya
  • 2,867
  • 1
  • 10
  • 24