Bug Hunter
Bug Hunter

Reputation: 45

Passing parameters to controller in laravel 5?

I am using Laravel 5 in one of my project and I want to pass some value to controller from route (web.php).

My code looks like:

Route Definition:

Route::get(Config::get('constant.ROUTES.UPLOADS'), 'UploadsController@index');

Value of constant

'UPLOADS' => '/images/{filename}'

Controller

class UploadsController extends Controller
{
    function index($filename) {
        //Some code here
    }
}

Error

ErrorException in UploadsController.php line 9:
Missing argument 1 for somewebsite\Http\Controllers\UploadsController::index()

Can anyone help me on this?

Upvotes: 1

Views: 2275

Answers (3)

Ramin
Ramin

Reputation: 322

In your controller you may also use the global request to get your value as well:

function index() {
        $yourFileName = $_REQUEST['filename'];
}

Upvotes: 1

Mayank Pandeyz
Mayank Pandeyz

Reputation: 26258

Problem exist in following line:

function index($filename) {
        //Some code here
}

Change it like:

public function index(Request $request, $id)
{
    $filename = $request->filename;
}

Check this reference

Note: To use Request:

use Illuminate\Http\Request;

Upvotes: 2

Miguel
Miguel

Reputation: 1887

Try like this:

function index(Request $request) {
   $filename = $request->filename; // the right value should be here
}

Be sure that you are using:

use Illuminate\Http\Request;

Upvotes: 3

Related Questions