Laravel 中的动态网址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13860162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:26:53  来源:igfitidea点击:

Dynamic urls in laravel?

phpdatabasedynamicrouteslaravel

提问by Hailwood

I am looking at switching to laravelfor my next project.

我正在考虑为我的下一个项目切换到 Laravel

My next project is probably going to be a small site with a few static pages, a blog and a projects manager and will be using controllers not routes.

我的下一个项目可能是一个包含几个静态页面、一个博客和一个项目经理的小站点,并且将使用控制器而不是路由。

What I am curious about is how I can manage dynamic routes in Laravel.

我很好奇的是如何在 Laravel 中管理动态路由。

Basically, I want to build in an admin section so I can easily create the static pages on the fly, and the static pages will have SEO focussed urls, e.g. http://domain.com/when-it-startedI do not want to have to create a new controller or route manually for each page.

基本上,我想建立一个管理部分,这样我就可以轻松地动态创建静态页面,并且静态页面将具有以 SEO 为重点的 url,例如http://domain.com/when-it-started我不想要必须为每个页面手动创建一个新的控制器或路由。

So I am wondering what the cleanest way is to handle this.

所以我想知道处理这个问题的最干净的方法是什么。

essentially all static pages are going to share the same view, just a few variables to change.

基本上所有静态页面都将共享相同的视图,只需更改几个变量。

The dynamic routing should work withthe controllers not instead of.

动态路由应该控制器一起工作而不是。

E.g. if we have a controller aboutwith a function staffthen this should be loaded via http://domain.com/about/staff

例如,如果我们有一个about带有功能的控制器,staff那么它应该通过http://domain.com/about/staff加载

but we dont have the function players, so a call to http://domain.com/about/playersshould check the database to see if a dynamic route exists and matches. If it does display that, otherwise show the 404 page. Likewise for a non-existant controller. (e.g. there would not be a when-it-startedcontroller!)

但是我们没有这个功能players,所以调用http://domain.com/about/players应该检查数据库以查看是否存在动态路由并匹配。如果确实显示,否则显示 404 页面。对于不存在的控制器也是如此。(例如,不会有when-it-started控制器!)

The chosen answer doesn't seem to work in Laravel 4. Any help with that?

所选答案似乎在 Laravel 4 中不起作用。对此有帮助吗?

采纳答案by Deji S

For Laravel 4 do this

对于 Laravel 4,请执行此操作

Route::get('{slug}', function($slug) {
    $page = Page::where('slug', '=', $slug)->first();

    if ( is_null($page) )
        // use either one of the two lines below. I prefer the second now
        // return Event::first('404');
        App::abort(404);

    return View::make('pages.show', array('page' => $page));
});

// for controllers and views
Route::get('{page}', array('as' => 'pages.show', 'uses' => 'PageController@show'));

回答by TLGreg

You could use the route wildcards for the job, you can start with an (:any)and if you need multiple url segments add an optional (:all?), then identify the page from the slug.

您可以为作业使用路由通配符,您可以从 an 开始,(:any)如果您需要多个 url 段,请添加一个 optional (:all?),然后从 slug 中识别页面。

For example:

例如:

Route::get('(:any)', function($slug) {
    $page = Page::where_slug($slug)->first();

    if ( is_null($page) )
        return Event::first('404');

    return View::make('page')->with($page);
});

回答by Adam Marshall

Very similar to Charles' answer, but in the controller:

与查尔斯的回答非常相似,但在控制器中:

public function showBySlug($slug) {
    $post = Post::where('slug','=',$slug)->first();
    // would use app/posts/show.blade.php
    return View::make('posts.show')->with(array(  
        'post' => $post,
    ));
}

Then you can route it like this:

然后你可以像这样路由它:

Route::get('post/{slug}', 'PostsController@showBySlug')
    ->where('slug', '[\-_A-Za-z]+');`

...which has the added bonus of allowing you an easy way to link straight to the slug routes on an index page, for example:

...它有一个额外的好处,让您可以轻松地直接链接到索引页面上的 slug 路线,例如:

@foreach ($posts as $post)
    <h2>{{ HTML::link(
        action('PostsController@showBySlug', array($post->slug)),
        $post->title
    )}}</h2>
@endforeach