【发布时间】: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”......你确定没有我真正需要的变量吗?对不起,我是初学者。