jeudi 4 octobre 2018

Laravel routing by looping through array

I am trying to use the Route::get method to setup the URL structure for my application without repeating Route::get for each item. I am doing this with a foreach loop as follows:

<?php

Route::get('/', function () {
    return view('welcome');
});
$pages = array('about', 'contact', 'faqs');  
foreach ($pages as $page) {
    Route::get($page, function () {
        return view($page);
    });
}

When run I come across the error: "Undefined variable: page". Although I have worked much with procedural PHP, and PHP within CMSs, I am fairly new to Laravel. What am I missing here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire