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

JBF's avatar
Level 3

Passing variables to the controller but not from the URI

For instance you have 3 static pages, index, about and contact. Each just has a view but you don't want to return that from the route because you want to use route caching.

Instead of making a controller method for each page, is it possible to do something like this in the web.php ...

Route::get('/', 'StaticController@showpage', ['page' => 'index']);
Route::get('/about', 'StaticController@showpage', ['page' => 'about']);
Route::get('/contact', 'StaticController@showpage', ['page' => 'contact']);

And then the controller method

public function showpage(Request $request, $page)
{
	return view($page);
}

I know the above doesn't work but hopefully it illustrates my point. Thanks!

0 likes
11 replies
yjuyjuy's avatar

You can do this if you only need a simple return view statement

Route::view('/', 'index');
Route::view('/about', 'about');
Route::view('/contact', 'contact');

or you can make a separate method for each route

Route::get('/', 'StaticController@index');
Route::get('/about', 'StaticController@about');
Route::get('/contact', 'StaticController@contact');
JBF's avatar
Level 3

Thanks but my question specifically asks if there is a way which avoids using a separate controller method and avoids simply returning a view. I know those options are available. I'm wondering if there is another way?

My example is just very simplistic. There have been a few times when I would like to hand my controllers a value directly from my routes file but I can't see how to do it. I also want to be able to use route caching, so I can't use a closure.

cookie's avatar

@jbf

Run:

php artisan make:controller PagesController --invokable

routes/web.php

Route::get('/{page}', 'PagesController');

Http/Controllers/PagesController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PagesController extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        return view($request->page);
    }
}
tykus's avatar

You can define one route that has an optional page wildcard. This must match an empty string (using a negative look-ahead), about or contact:

Route::get('/{page?}', 'StaticController@showpage')
	->where('any', '((?![\s\S])|about|contact)');

Then in the controller, use the $page to locate the view, if it was defined, or replace with index

public function showpage($page = null)
{
	return view($page ?? 'index');
}

~Not tested, but it should do the trick...~

Tested, and the negative look-ahead is too lenient

Snapey's avatar

Your overthinking it. If you don't need to prep any data for the pages, just use Route::view and move on.

JBF's avatar
Level 3

Hey guys, thanks for the suggestions. I’m afraid the example was too simplistic. Let me explain a different way. I’ve come across various situations where I would like to do some minor customisation which is dependent on the route and although I could easily push it to the database, or have extra controller methods, some times it would just be easier to pass a parameter from the route definition. I’ve had a break from coding for years but I used to do exactly this with symfony 1.4 where you could easily add extra parameters to the request object from your route file independently from any uri params. In those old days it was a yaml file. A simple example would look like this…

billing_address_show:
  url:                      /billing-address/:id
  param:                    { module: Address, action: Show, type: billing }

delivery_address_show:
  url:                      /delivery-address/:id
  param:                    { module: Address, action: Show, type: delivery }

Both the id and the type parameters could then be grabbed from the request object.

JBF's avatar
Level 3

Thanks but I don't want them to be part of the uri. The user should not be able to see these values. They would just be internal.

artcore's avatar

Maybe from the request path

Route::get('/{any}', 'PageController@show')->where('any', '.*');
//top route wins so this could be last in your routes file

PageController::show(Request $request)

return view(string $request->path()) //could be pathname() I forgot.

try catch would be handy

JBF's avatar
Level 3

I just figured it out by looking at the symfony docs. You can do this...

Route::get('/foobar', 'FooController@bar')->defaults('message', 'hello world');

Now I can use request('message') in the controller.

Thanks for your help everyone.

1 like

Please or to participate in this conversation.