Support the ongoing development of Laravel.io →
posted 10 years ago
Views

Hi,

Sorry for my bad english (I'm french and not very good in english)

I have setup several pages in my views/pages folder. Instead of create one route per page (and thus the page itself), I just want to create the page and then parse the pages folder to create the route.

In routes.php I have the following code :

$dirname = base_path().'/app/views/pages/';
$dir = opendir($dirname); 
$filelist = array();
while($file = readdir($dir)) {
	if($file != '.' && $file != '..' && !is_dir($dirname.$file))
	{
		$filearray = explode('.', $file);
		array_push($filelist, $filearray[0]);
	}
}
closedir($dir);

foreach ($filelist as $page) {
	Route::get(
		$page, 
		function()
		{
			return View::make('home');
		}
	);
}

The return View::make($page) statement doesn't recognize my $page variable, how can I do?

Last updated 2 years ago.
0

Try this.

foreach ($filelist as $page) {
    Route::get(
        $page, 
        function() use ($page)
        {
            return View::make( $page );
        }
    );
}
Last updated 2 years ago.
0

Thanks a lot it works!

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

edubuc edubuc Joined 17 May 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.