获取 laravel 中域名后面的值

Get the value which comes after the domain name in laravel

如何在 laravel 中为下面的第二个选项创建路线....

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

1st 它重定向到主页,这对我来说是正确的。

2nd 我需要得到 8048/

之后的内容

所以基本上 content/645668/nice-up-civic-poll.html 是一个参数,我需要单独处理它及其动态 link.

laravel 中的路线 api:

路线::get('/', 'HomeController@index');

www.example.com 将加载包含所有故事的主页。

下面的links作为例子应该得到www.example.com/之后的值,基本上它是一个story/article link所以当它出现时特定的故事将被显示。

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

所以基本上是在使用 apache 服务器的域名之后获取所有内容。

.htaccess 文件

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

尝试使用 $request 助手..

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

或者试试这个..

$request->getRequestUri();

使用$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

您可以使用 php 的内置函数 parse_url 来检索 content/645668/nice-up-civic-poll.html.

parse_url($url, PHP_URL_PATH)

您可以使用 Request::path() 获取当前 url。

https://laravel.com/api/5.5/Illuminate/Http/Request.html - 勾选此项以获得 Request 可用的所有选项。
例如:如果您只想检查用户是否在某些 url 中,请使用此 - Request::is('/url') // This will return true or false

如果您想要一个主路由,然后每个其他 URI 都转到一个控制器方法,您可以创建一个包罗万象的路由:

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

这将捕获与任何先前定义的路由不匹配的任何 URI。

如果你想让所有东西都到一个地方:

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

Post 关于创建捕获所有路由,在参数上使用正则表达式条件回答:

更新:

如果您需要捕获的这些 URI 都具有相同的格式,

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

您可以注册一个路由来匹配该格式,而不是捕获所有可能的内容:

Route::get('content/{id}/{slug}', function ($id, $slug) {
    ...
});