dongtang1997 2012-12-13 12:46
浏览 47
已采纳

laravel中的动态网址?

I am looking at switching to laravel for my next project.

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.

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-started I do not want to have to create a new controller or route manually for each page.

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 with the controllers not instead of.

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

but we dont have the function players, so a call to http://domain.com/about/players should 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-started controller!)

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

  • 写回答

3条回答 默认 最新

  • dongmou1964 2013-04-03 11:42
    关注

    For Laravel 4 do this

    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'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算