【问题标题】:How to get a slug to controller?如何让一个蛞蝓到控制器?
【发布时间】:2017-12-19 15:42:04
【问题描述】:

我是 Laravel 的新手。我想问一下,我怎样才能在控制器中找到一个slug? 例如,我的实际网址是 www.example.com/contact-us

我需要在控制器的变量中获取“contact-us”值。有什么简单的方法吗?

在另一个网址上,例如 www.example.com/faq 我需要在同一个变量中包含值“faq”。我该怎么做?非常感谢

使用 laravel 5.5

这就是我的路由文件中的内容:

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/podmienky-pouzitia', 'StaticController@index');
Route::get('/faq', 'StaticController@index');
Route::get('/o-projekte', 'StaticController@index');

这就是我的 StaticController 文件中的内容:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\StaticModel;

class StaticController extends Controller
{
    public function index($slug) {
        var_dump($slug); // I need to get a variable with value of my actual slug
    }
}

【问题讨论】:

  • 这个“slug”不应该是你的控制器的一个动作吗?
  • documentation 有很多关于路由的细节。你试过什么?
  • 没有。我想创建类似“staticPage”控制器的东西,它只会打印内容。没有其他的。我不想为每个静态子页面创建一个控制器:O 所以我想拿一个 slug 并通过 slug 找到 DB 中的行,这样我就可以打印文本了。我有大约 12-13 个静态子页面,所以我认为创建 12 个控制器会非常愚蠢,它们会做同样的事情。所以我需要的只是变量的名称,我的 slug 将被保存在其中,我可以在 Controller 中使用它。当然,如果存在的话……
  • 你知道你的routes can have parameters 对吗?你可以有一个路由/staticpages/{slug}(或其他东西),并让它由一个接受$slug作为参数的控制器动作处理
  • 我不想在 URL 中包含“staticpages”这个词。因为“seo”......你确定没有我真正需要的变量吗?对不起,我是初学者。

标签: php laravel-5


【解决方案1】:

您可以将静态页面 slug 设为参数:

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/{slug}', 'StaticController@index'); //This replaces all the individual routes

在静态控制器中:

 public class StaticController extends Controller {
     public function index($slug) {
          // if the page was /faq then $slug = "faq"              
     }
 }

但是要注意声明路由的顺序很重要。因此,您必须在最后的一般“catch-all”路线之前声明所有其他路线。

【讨论】:

    【解决方案2】:

    与其为每个页面/slug 声明多个路由,不如只声明一个带有路由参数的路由,例如:

    Route::get('/{slug}', 'StaticController@index');
    

    此时index方法会在$slug参数中接收到slug,例如:

    public function index($slug)
    {
        var_dump($slug);
    }
    

    所以现在,您可以使用如下方式发出请求:

    http://example.com/faq // for faq page
    http://example.com/contact-us // for contact page
    

    所以,$slug 现在将包含faq/contact-us 等等。但是,在这种情况下,您会遇到问题,例如,http://exampe.com/home 也类似于带有 slug 的动态路由,因此,如果您在动态路由之前声明 home 路由(带有 {slug} 的路由),那么StaticController@index 将被调用,因此您可以在父命名空间下使用 slug 声明动态路由,例如:

    Route::get('/static/{slug}', 'StaticController@index');
    

    因此,如果您有一些带有这些 slug 的预定义静态页面,您可以轻松区分路由或使用 slug 参数声明动态路由,并在路由声明中添加 where 约束。 Here is a somewhat similar answer,可能会有所帮助。另外,请查看有关route constraints 的更多信息。

    更新:您还可以使用以下内容添加路由约束:

    Route::get('/{slug}', 'StaticController@index')->where('slug', 'faq|contact|something');
    

    上面的声明只会匹配下面的url:

    http://example.com/faq
    http://example.com/contact
    http://example.com/something
    

    【讨论】:

      【解决方案3】:

      您可以使用 action() 辅助函数来获取指向给定控制器和方法的 URL。

      routes.php:

      Route::get('/home', 'HomeController@index')->name('home');
      

      HomeController.php:

      class HomeController extends Controller {
          public function index (Request $request) {
              $url = action('HomeController@index');
              // $url === 'http://www.example.com/home'
      
              // OR
      
              $path = $request->path();
              // $path === 'home';
          }
      }
      

      如果您使用第一种方式,则可以使用request() 帮助器(或Request 实例)从字符串中删除域:

      $url = str_replace(request()->root(), '', $url);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-25
        • 2016-03-31
        • 2013-09-16
        • 2017-11-05
        • 2014-07-30
        • 2016-06-02
        • 2021-04-04
        • 2014-04-02
        相关资源
        最近更新 更多