Php Laravel 5应用中的动态路由

Php Laravel 5应用中的动态路由,php,laravel,laravel-5,Php,Laravel,Laravel 5,我希望有人能帮助我的动态路由的网址,可以有多个部分。我一直在网上搜索,但没有找到任何东西能帮助我解决我的具体情况 有点背景。。。几年前,我为定制客户端网站开发了一个基于CodeIgniter的CMS包。这个CMS包有几个模块(页面、博客、日历、查询等)。对于Pages模块,我将路由缓存到一个“CustomRoutes”配置文件中,该文件将页面的完整路由(包括父级、祖父母等)与页面ID相关联。我这样做是为了不必通过数据库查找来找到要显示的页面 我目前正在使用Laravel(5.1)[在学习Lara

我希望有人能帮助我的动态路由的网址,可以有多个部分。我一直在网上搜索,但没有找到任何东西能帮助我解决我的具体情况

有点背景。。。几年前,我为定制客户端网站开发了一个基于CodeIgniter的CMS包。这个CMS包有几个模块(页面、博客、日历、查询等)。对于Pages模块,我将路由缓存到一个“CustomRoutes”配置文件中,该文件将页面的完整路由(包括父级、祖父母等)与页面ID相关联。我这样做是为了不必通过数据库查找来找到要显示的页面

我目前正在使用Laravel(5.1)[在学习Laravel时]重建这个CMS包。我需要先弄清楚路由情况,然后才能在新版本的包中继续使用Pages模块

我知道我可以做一些像

// routes.php
Route::get('{slug}', ['uses' => 'PageController@view']);

// PageController.php
class PageController extends Controller
{
    public function view($slug)
    {
        // do a query to get the page by the slug
        // display the view
    }
}
如果我不允许嵌套页面,这会起作用,但我允许。我只基于父级强制执行slug的唯一性。因此,可能会有多个页面包含一段法戈

  • 地点/法戈
  • 职员/法戈
与我使用CodeIgniter构建的包一样,我希望能够避免额外的数据库查找以找到要显示的正确页面

我最初的想法是创建一个配置文件,该文件将具有与旧版本系统相同的动态路由。路由只会在特定时间更改(当创建页面、修改slug、更改父级时),所以“缓存”它们将非常有效。但我对拉威尔还是新手,所以我不确定最好的方法是什么

我确实设法弄明白了以下路线是可行的。但这是最好的方法吗

Route::get('about/foobar', function(){
    return App::make('\App\Http\Controllers\PageController')->callAction('view', [123]);
});

Route::get('foobar', function(){
    return App::make('\App\Http\Controllers\PageController')->callAction('view', [345]);
});
基本上,我希望在创建页面时(或者更改slug或父级时),将特定路由绑定到特定的页面ID

我只是把事情复杂化了吗

任何有关这方面的帮助或指导都将不胜感激


谢谢

我处理这个问题的方法是使用两条路径,一条用于主页(通常包含更复杂的逻辑,如新闻、文章、横幅等),另一条用于任何其他页面

路线 上面要注意的重要部分是链接在路由末尾的
->where()
方法。这允许您声明路由参数的正则表达式模式匹配。在本例中,我允许
{slug}
参数使用字母数字字符、连字符和前斜杠

这将匹配鼻涕虫,如
测试页面

测试页/子页

另一页/子页

页面控制器方法 我将模板文件信息保存在数据库中,因为我允许用户在内容管理系统中创建模板


然后,来自数据库查询的响应被传递到视图,在该视图中,它可以输出到元数据、页面、面包屑等。

我也在寻找关于在laravel中创建动态路由的相同答案,我得出了以下结论: 在routes.php中

<?php


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

$str=Request::url();
$del="/public/";
$pos=strpos($str, $del);
$important1=substr($str, $pos+strlen($del), strlen($str)-1);
$important=ucfirst($important1); 
$asif=explode("/", $important);
$asif1=explode("/", $important1);
//echo $important;
$post=$asif1[0];
$post1=$asif1[1];
if(isset($asif1[2]))
{
   $post2=$asif1[2];
}
if(!(isset($post2)))
{
   Route::match(array('GET','POST'),$important1, $asif[0].'Controller@'.$asif[1]);
}
if(isset($post2))
{      Route::match(array('GET','POST'),$post.'/'.$post1.'/{id}',$asif[0].'Controller@'.$asif[1]);
}
Route::get('/', function () {
    return view('welcome');
});

我只是把事情复杂化了吗?
,IMO
是的
。此外,这些路由看起来是静态的,如果不允许为
ID
s使用任何占位符,就没有动态性。每个路由只绑定到一个特定的
id
。根据我的设置计划,我认为没有任何方法可以使用动态路由,而不需要我进行额外的数据库查询来找到正确的页面。我会对其他模块使用动态路由,比如博客,但对于常规页面,我认为它不起作用。正如我在问题中提到的,如果两个不同的页面有不同的父页面,那么它们可能有相同的slug。所以我不能按你的建议去做。我不想为了找到正确的父级而进行多次查找。@Becky数据库中的slug列将包含完整的slug。即
位置/fargo
。这样你就不必担心复制品了。你为什么只使用最后一段作为你的鼻涕虫呢?我只是想对你的回答说声谢谢。我花了好几个小时试图找到一个好方法来动态处理到Laravel5.2中的子页面的路由。您使用
->where()
方法的建议解决了这个问题,该方法允许在slug中使用前斜杠。虽然这有点不言自明,但我还可以补充一点,像这样的“一网打尽”的路由应该(必须?)放在所有其他路由之后,这样,例如
admin/pages
就不会被捕获为该路由的slug。谢谢<代码>->其中('slug','([A-Za-z0-9\-\/]+)
是我拼图中缺失的一块。哦,天哪,你是天才,你帮了我的忙,谢谢!:)
public function index()
{
    $page = Page::where('route', '/')->where('active', 1)->first();

    return view($page->template)
        ->with('page', $page);
}

public function getPage($slug = null)
{
    $page = Page::where('route', $slug)->where('active', 1);

    $page = $page->firstOrFail();

    return view($page->template)->with('page', $page);
}
<?php


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

$str=Request::url();
$del="/public/";
$pos=strpos($str, $del);
$important1=substr($str, $pos+strlen($del), strlen($str)-1);
$important=ucfirst($important1); 
$asif=explode("/", $important);
$asif1=explode("/", $important1);
//echo $important;
$post=$asif1[0];
$post1=$asif1[1];
if(isset($asif1[2]))
{
   $post2=$asif1[2];
}
if(!(isset($post2)))
{
   Route::match(array('GET','POST'),$important1, $asif[0].'Controller@'.$asif[1]);
}
if(isset($post2))
{      Route::match(array('GET','POST'),$post.'/'.$post1.'/{id}',$asif[0].'Controller@'.$asif[1]);
}
Route::get('/', function () {
    return view('welcome');
});