Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NinjaJoe's avatar

Passing Values from Routes to Controller Functions

Hi guys! I've been using the following code to send variables from my web routes to my controller functions for years. It works, but the code is pretty ugly. routes/web.php:

Route::get( 'about-us', [ 'uses' => 'MyController@about_us', 'foo' => 'bar', 'page' => 'about' ] )->name( 'about-us' );

Receive the values in my controller, app/Http/Controllers/MyController.php:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
	public function about_us( Request $request ) {
		$foo = $request->route()->getAction()[ 'foo' ];
		$page = $request->route()->getAction()[ 'page' ];

		return view( 'about-us' );
	}
}

Is there a cleaner, newer ways to do this? Thank you for any help.

0 likes
12 replies
MichalOravec's avatar

It's the same as, so I don't know what the hell it's good for.

$foo = 'foo';

$page = 'page';
automica's avatar

@ninjajoe apart from wondering why you would be passing variables to a method via a get route, I would simplify the method

public function aboutUs(Request $request) {

		$foo = $request->foo;
		$page = $request->page;

		return view('about-us', compact('foo','page'));
	}
NinjaJoe's avatar

How would you pass the variables in your routes script?

automica's avatar

@ninjajoe I would do something like this:

Route::get('user/{id}', function ($id) {
    return 'User '.$id;
});

with

http://mydomain.com/user/1

or

Route::get('posts/{post}/comments/{comment}', function ($postId, $commentId) {
    //
});

with

http://mydomain.com/posts/1/comments/2

if you are just looking to pass arguments into a view, and don't need anything specific in your controller, then look at View Routes:

Route::view('/welcome', 'welcome');

Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

https://laravel.com/docs/8.x/routing#view-routes

NinjaJoe's avatar

Avoiding Optional Route Parameters was my main objective. Is there any cleaner way to do it than my original post? The values must be passed from Route::get( ); not Route::view( );.

automica's avatar

@ninjajoe the parameter would only be optional if it had a questionmark after it

eg

Route::get('user/{name?}', function ($name = null) {
    return $name;
});

otherwise its compulsory

Route::get('user/{name}', function ($name) {
    return $name;
});

for your original route

Route::get('about-us/{foo}/{page}', [MyController::class, 'about_us'])->name('about-us');
NinjaJoe's avatar

@automica Sorry, avoiding route parameters is what I meant. These values are not meant to be passed from a route parameter, but rather from variables I'm importing from an included PHP file.

Sinnbeck's avatar

Why does the route need to know anything about it? Can't the controller handle it directly?

1 like
NinjaJoe's avatar

Looks like I already have the best way to do it then. Although I was able to make the code more succinct in the controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MyController extends Controller
{
	public function about_us() {
		$foo = request()->route()->getAction( 'foo' );
		$aaa = request()->route()->getAction( 'aaa' );

		return view( 'about-us', [ 'foo' => $foo, 'aaa' => $aaa ] );
	}
}

The route part I wasn't able to improve, though. Maybe I already have the best way there.

1 like
automica's avatar

Can you explain what the

getAction( 'foo' );

responds to?

It might be easier trying to explain what you are doing, using real data rather than foo and bar?

What are you trying to display in your aboutUs method? Where are you getting the data you are passing into your blade?

Please or to participate in this conversation.