Search code examples
phplaravellaravel-3

Laravel3: How can I forward a string to a function?


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?


Solution

  • 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');
        });