Laravel 4 获取要在url栏中显示的url数据

Laravel 4 获取要在url栏中显示的url数据,laravel-4,Laravel 4,我使用的是Laravel4,我试图让url栏显示保存在数据库中的文本url,而不是使用id 这是我的routes.php Route::get('/{id}', function($id = 1){ if(is_numeric($id)) { $page = Menu::find($id); $action = 'content'; return App::make('HomeController')->$action($id); } else { $c

我使用的是Laravel4,我试图让url栏显示保存在数据库中的文本url,而不是使用id

这是我的routes.php

Route::get('/{id}', function($id = 1){

if(is_numeric($id))
{

    $page = Menu::find($id);
    $action = 'content';
    return App::make('HomeController')->$action($id);
} else {
    $column = 'url';
    $url = Seo::where($column, '=', $id)->get();
    $action = 'show';
    return App::make('HomeController')->$action($url[0]->id);
}   
});
我还使用一个透视表将菜单链接到seo

搜索引擎优化模型

<?php

class Seo extends \Eloquent {
protected $fillable = array('url', 'meta_title', 'meta_description', 'keywords');
protected $guarded = array('id');

protected $table = 'seo';

public static $rules = array(
    'title' => '',
    'content' => '',
    'image' => ''
);

public function menu(){
    return $this->belongsToMany('Menu', 'menu_seo', 'seo_id', 'menu_id');
}
}
然后我调用引用菜单的视图,如下所示

@foreach($menu->banner as $banners)
{{ $banners->title }}
@endforeach

我仍然不确定您希望如何检索与id关联的正确URL,但这并不重要。基本原则是获取URL并进行重定向:

Route::get('/{id}', function($id = 1){
    if(is_numeric($id))
    {
        $page = Menu::find($id);
        $url = 'foo'; // get correct URL somehow
        return Redirect::to($url);
    } else {
        // ...
    }   
});

你的问题是什么?对不起,我以为我在上面。我想将id更改为我添加到数据库中的url,所以您想从/1重定向到/foo-bar吗?或者类似的东西,我不明白如果id是菜单id,你如何得到正确的url?因为这是一个多对多关系…我得到了正确的url,因为我的原始路由是[code]route::get'/{id}','HomeController@content';[/code]我还更新了我的问题,使其具有我的控制器,但某个菜单id可以指定许多Seo模型。可以有不同的URL。那么,当访问/123时,您希望重定向到哪个URL?我尝试过,但现在出现了这个错误。没有模型[菜单]的查询结果。我认为这意味着数据库中没有任何内容,但这是不对的,因为数据库中有数据。如果按未命名为id的列进行搜索,则除非在模型中指定主键,否则不能使用find$id。也许这就是问题所在……这就是我得到url$page=Menu::where'id',$id->first的方法;foreach$page->seo as$seo{$url=$seo->url;}这确实得到了正确的url,但我抛出了错误。您是否仍在调用content方法?因为这个错误通常只从FirstorFaily抛出,所以我正在调用content方法
@foreach($menu->banner as $banners)
{{ $banners->title }}
@endforeach
Route::get('/{id}', function($id = 1){
    if(is_numeric($id))
    {
        $page = Menu::find($id);
        $url = 'foo'; // get correct URL somehow
        return Redirect::to($url);
    } else {
        // ...
    }   
});