Question

I am trying to generate routes from one template "tuning.blade.php"

I have a DB with 250 rows I would like to dynamically create 250 routes with one route or one controller.

I want to be able to use these URLs

laravel.dev/tuning/(field from DB row 1)
laravel.dev/tuning/(field from DB row 2 and so on)

I want to put the DB requests in tuning.blade.php so that this template can display all 250 rows using 250 different URLS

I have tried to use the first example from Laravel Docs

class UserController extends BaseController {

    /**
     * Show the profile for the given user.
     */
    public function showProfile($id)
    {
        $user = User::find($id);

        return View::make('user.profile', array('user' => $user));
    }

}


Route::get('user/{id}', 'UserController@showProfile');

I also got some interesting search results from http://forumsarchive.laravel.io/viewtopic.php?id=9010 But alsas I always end up with a notfound exception

But I am unsure what to put in my tuning template to display anything at all. my Tuning template reside in app/views/home/tuning.blade.php

Currently I have got the error "Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException"

Can anyone put me in the right direction of where I can find a resource to help me understand?

Was it helpful?

Solution

You said you want to be able to use these URLs:

laravel.dev/tuning/(field from DB row 1)
laravel.dev/tuning/(field from DB row 2 and so on)

You can do this by declaring a route like this:

Route::any('/tuning/{field}', 'TuningController@someMethod'); you may get/post

You shouldn't run sql queries from your view and if you want to really declare some dynamic routes for each fields from your database then you may do id right from your routes.php file, for example, assume that you have a table named tunings and that table contains some fields including id, name and some others. Now, to declare routes individually for each routes dynamically using the tuning field of table tunings you may create a method in your TuningController, something like this:

class TuningController extends baseController {
    // other methods...

    public function registerTuningRoutes()
    {
        $tunings = Tuning::all(); // Assume that you have a model Tuning

        // Or you may use this instead
        $tunings = DB::table('tuning')->get();

        // Now loop all tunings and declare routes
        foreach($tunings as $tuning) {
            $url = '/tuning/' . $tuning->name;
            $route_name = 'tuning.' . $tuning->name;
            Route::any($url, $route_name); // You may use get/post
        }
    }

    public function TuningMethod($tuning = null)
    {
        // $tuning will contain the current tuning name, check
        dd($tuning);
    }
}

Now in your routes.php file use something like this:

Registers route for each tuning name in database
App::make('TuningController')->registerTuningRoutes();

From your terminal/command prompt check the routes by running following command:

php artisan routes

But, I think, you don't need to do this, only one route is enough as I mentioned earlier in my answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top