dpwh11290 2013-04-07 21:46
浏览 8
已采纳

Laravel3:如何将字符串转发给函数?

I have something like this:

Route::get('admin/user/modify', 'admin@user_modify');

But I should convert it to something like this:

Route::get('admin/user/modify/(:string)', 'admin@user_modify (with $string)');

Basically, I want to pass a string variable to my function.

How can I do this?

One more thing, what if the user doesn't pass a value and only browse admin/user/modify/, what will happen then? Should I manually check it or can I return it to 404 etc. if the value is not entered?

  • 写回答

1条回答 默认 最新

  • dua27031 2013-04-07 21:49
    关注

    Try something like this:

    In your routes.php:

    Route::get('users/edit/(:any)', array('as' => 'edit_user', 'uses' => 'users@edit'));
    

    In your controller:

    public function get_edit($id)
    {
         return View::make('users.edit')
            ->with('user', User::find($id))
            ->with('title', 'Edit User');
    }
    

    if the user doesn't pass a value and only browse admin/user/modify/ will show a 404. If you want another behaviour you can do something like this:

        //Return a 404 response or what you prefer
        Route::get('admin/users/update', function(){
            return Response::error('404');
        });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用