douhei8633 2014-03-07 19:52
浏览 64

缺少一些必需参数(“url_tag”)来生成路由“customUrl”的URL。 Laravel 4

I am trying to break out a resource ("fans") into separate routes so I can have custom urls for the individual resource pages, as opposed to using just $id.

So instead of this:

Route::resource('fans', 'FansController');

I have broken it into this:

Route::get('fans/{url_tag}', array('as' => 'customUrl', function($url_tag)
{
    $fan = Fan::where('url_tag','=',$url_tag);
    return View::make('fans.show', compact('fan'));
}))
->where('url_tag', '[A-Za-z]+');

Route::get('fans/{id}', function($id)
{
    // do something with $id or
    return Redirect::route('customUrl');
})
->where('id', '[0-9]+');

In the fans controller, I have this:

public function show($id)
        {
        $fan = Fan::find($id);      
        return View::make('fans.show', compact('fan'))
            ->with('fans', Fan::all())
            ->with('latest_fan_likes', Fanartist::latest_profile_fan_likes($id));
}

Basically I would like the page /fans/$id to appear as /fans/$url_tag (where url_tag is another column in the db, that is unique). But I would like it to have all of the properties of the resource, so I can call $fan->column directly.

When I run this, I get the error:

Some mandatory parameters are missing ("url_tag") to generate a URL for route "customUrl".

Any idea what is going on or what is wrong? I am using Laravel 4. Thank you.

  • 写回答

1条回答 默认 最新

  • dqzpt40064 2014-03-07 20:32
    关注

    You probably are trying to generate an URL, somewhere else in your code, in a controller or even a view, something like (may not be exactly that!):

    URL::route('customUrl');
    
    or 
    
    route('customUrl');
    

    But you need to provider the url_tag:

    URL::route('customUrl', array('url_tag' => 'something');
    

    For you could be something like:

    URL::route('customUrl', array('url_tag' => $fan->url_tag);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件