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

garrettmassey's avatar

Passing variables through redirect()->route('route.name') (SOLVED)

EDIT: I genuinely don't know what changed, but it is working now. I cleared my cache, and then just rewrote the return as:

return redirect()->route('items.index')->with('success', 'Item was created');

And it is working exactly as expected. No issues.

In a controller, in the store method, I am trying to use

	return redirect()->route('items.index', ['page_title' => 'Items'])->with('success', 'Item was successfully saved');

but when I use this code, I get the error:

	Undefined variable: page_title

The page title is in a partial blade template that is included in the main template. so the variable page_title isn't in the index.blade.php template for the item, it is instead in a different blade template in a different directory.

0 likes
10 replies
tykus's avatar

The array argument is used to pass values for the URL wildcard segments needed by the Route; it is not used for passing arbitrary data.

If you were to visit the URL directly; how would the $page_title variable be set?

garrettmassey's avatar

@tykus In the controller, the variable $page_title is set like so:

public function index()
{
    return view('int-layouts.controlpanel.panels.items.index', [
        'page_title' => 'Items',
        'slug' => 'items_home',
    ]);
}

Both the page_title and the slug are passed through the view and used to generate breadcrumbs automatically for the view that is being loaded.

Is it possible to pass arbitrary data through named routes? I have used the array argument to pass wildcard segments, but this is the first time I have tried to pass arbitrary data.

garrettmassey's avatar

It is also worth noting that I have tried this method as well:

return redirect()->action('ProviderController@index');

but the error persists.

tykus's avatar

@garrettmassey you can append query parameters as described by @webrobert but it is not particularly nice for a page title variable.

Just for clarity, is that code your current implementation? Is $page_title undefined in that scenario?

EDIT how is the partial template included in the int-layouts.controlpanel.panels.items.index view template?

1 like
garrettmassey's avatar

@tykus Yes, the current implementation has the $page_title undefined when using redirect()->action()

Here is the structure of the blade directories, along with some of the code:

Resources > views > int-layouts > partials > master.blade, heading.blade, wrapper.blade

master.blade.php:

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no, maximum-scale=1.0, user-scalable=no">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="description" content="">
		<meta name="author" content="">
		<link rel="shortcut icon" href="{{URL::asset('favicon.ico')}}" type="image/x-icon">
		<title>Controlpanel</title>
	
		<!-- Stylesheets -->
	</head>
	<body>
		@yield('wrapper')
		@include('int-layouts.partials.scripts')
	</body>
</html>

The 'wrapper' is in Resources/views/int-layouts/partials/ and is set up like so:

@extends('int-layouts.partials.master')

@section('wrapper')
	<div class="be-wrapper">
		<!-- TOP NAV -->
		@include('int-layouts.controlpanel.nav.index')
		<!-- END TOP NAV -->

		<!-- LEFT NAV -->
		@include('int-layouts.controlpanel.nav.left-nav.index')
		<!-- END LEFT NAV -->

		<!-- PANEL WRAPPER -->
		<div class="be-content">
	 
	    	@include('int-layouts.partials.header')
		
			@yield('error')
		
			<div class="main-content container-fluid">
				<div class="row">
				
					<!-- MESSAGES / ERRORS -->
						@include('int-layouts.partials.message')
					<!-- END MESSAGES / ERRORS -->

					<!-- PANEL CONTENT -->
						@yield('panel')
					<!-- END PANEL CONTENT -->

				</div>
			</div>
		</div>
		<!-- END PANEL WRAPPER -->
	</div>
@endsection

And the line @include('int-layouts.partials.header') is where the $page_title is used. I have it set up like that so that every page the user accesses has automatically generated breadcrumbs.

The @yield('panel') is where the specific page content is generated, like the int-layouts.controlpanel.panels.items.index

garrettmassey's avatar

It's also worth noting that return $redirect()->back()->with('success', 'success message'); works just fine, and does not give any errors.

webrobert's avatar

@garrettmassey,

I assume you read this? https://laravel.com/docs/8.x/routing#generating-urls-to-named-routes

How are you defining the page_title attribute in the controller?

EDIT: to clarify for everyone. From the docs...

If you pass additional parameters in the array, those key / value pairs will automatically be added to the generated URL's query string:

$url = route('profile', ['id' => 1, 'photos' => 'yes']);

// /user/1/profile?photos=yes

this also works whether or not there are defined parameters for the ROUTE

garrettmassey's avatar

@webrobert If I used this method, wouldn't I have to set the $page_title = $_GET['page_title'] in my view? And if that is the case, would I have to change how I pass in the $page_title for every page because that variable is used in a partial blade template?

anilkumarthakur60's avatar

your route must be

Route::get('/items/get/{page_title},[ControllerName:class,'method'])->name('items.index');

to return redirect above route

return redirect()->route('items.index', ['page_title' => 'Items'])->with('success', 'Item was successfully saved');

Please or to participate in this conversation.