-1

how to create route in laravel for the below 2nd option....

  1. http://localhost:8048/
  2. http://localhost:8048/content/645668/nice-up-civic-poll.html

1st it redirects to home page which is correct for me.

2nd I need to get what ever comes after 8048/

so basically content/645668/nice-up-civic-poll.html is a parameter which I need to deal with it separately and its dynamic link.

Route api in laravel :

Route::get('/', 'HomeController@index');

www.example.com will load home page with all stories.

The below links as an example should get the value after www.example.com/ basically its a story/article link so when that comes specific story will be displayed.

www.example.com/content/645668/nice-up-civic-poll.html

www.example.com/content/283206/something-here.html

www.example.com/content/234323/good-nice.html

www.example.com/content/451425/breakup-actor.html

www.example.com/content/365412/accident-occured.html

So basically get everything after domain name which is using apache server.

.htaccess file

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteRule ^ index.php [L]
</IfModule>
Sharath
  • 2,348
  • 8
  • 45
  • 81

5 Answers5

1

try using $request helper..

$leftbehind = str_replace($request->getHost(),'', $request->fullUrl());

or try this..

$request->getRequestUri();
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • But it doesn't hit any api in laravel so that i can somehow get the value of it... rather it displays 404 not found page – Sharath Dec 01 '17 at 15:39
1

If you want a home route and then every other URI to go to a single Controller method you can make a catch-all route:

Route::get('{catch}', 'SomeController@action')->where('catch', '.*');

This would catch any URI that doesn't match any previously defined route.

If you want everything to go to one place:

Route::get('{catch?}', ....)->where(...); // optional param

Post about creating a catch all route, answer using regex conditions on parameter:

SO - How do I make a Catch-All Route in Laravel 5.2

Update:

If these URIs you need to catch all have the same format,

www.example.com/content/645668/nice-up-civic-poll.html

you can register a route to match that format instead of catching everything possible:

Route::get('content/{id}/{slug}', function ($id, $slug) {
    ...
});
lagbox
  • 48,571
  • 8
  • 72
  • 83
  • 1
    'not working' ... means you have an expectation of something and yet you are getting a different result ... you have to articulate what those are, as 'not working' doesn't have any context – lagbox Dec 01 '17 at 18:28
  • @lagobx the code which you gave doesn't work still I get laravel 404 error. To better understand i have provided with .htaccess file and examples in my original question – Sharath Dec 01 '17 at 18:41
  • what 404, apache 404 or laravel 404? – lagbox Dec 01 '17 at 18:48
0

Use $request->path()

The path method returns the request's path information. So, if the incoming request is targeted at http://example.com/foo/bar, the path method will return foo/bar

https://laravel.com/docs/5.5/requests#request-path-and-method

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • But it doesn't hit any api in laravel so that i can somehow get the value of it... rather it displays 404 not found page – Sharath Dec 01 '17 at 15:39
0

You can use The Request::path() to get the current url.

https://laravel.com/api/5.5/Illuminate/Http/Request.html - Check this for all the options available for Request.
eg: If you want to just check whether the users is in some url or not use this - Request::is('/url') // This will return true or false

Advaith
  • 2,490
  • 3
  • 21
  • 36
-1

You can use php's built in function, parse_url to retrieve content/645668/nice-up-civic-poll.html.

parse_url($url, PHP_URL_PATH)
plmrlnsnts
  • 1,644
  • 12
  • 10
  • But it doesn't hit any api in laravel so that i can somehow get the value of it... rather it displays 404 not found page – Sharath Dec 01 '17 at 15:39
  • @Sharath If that URL doesn't go to Laravel, you've misconfigured your server. Is it Laravel's 404 or the server's? – ceejayoz Dec 01 '17 at 15:58
  • I think what the op wants is something like `Route::get('/{slug}', ...);` where slug can contain content/645668/nice-up-civic-poll.html. Correct me if I'm wrong. – plmrlnsnts Dec 01 '17 at 16:02
  • @ceejayoz it is laravel's 404 error... its treating /content/124553/ as a path but its a param for me... – Sharath Dec 01 '17 at 17:14
  • Why dont you reconsider you url structure and pass your /content/12345... as url param instead? – plmrlnsnts Dec 01 '17 at 17:26
  • @PaulSantos can't do because from the mobile apps it redirects to www.example.com/content/....... in this manner. So mobile apps and other places needs to be updated which is not possible. Earlier it was written in plain php which was working fine... Now we have converted to laravel. – Sharath Dec 01 '17 at 18:00
  • Can you try this brother, 'Route::get('/{path?}, 'controller@func')->where('path', '.*');' – plmrlnsnts Dec 02 '17 at 01:45