Question

How do I make the controller display in my blade layouts?

I have 250 dynamic routes imported from MySQL. They are produced using the following controller.

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

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

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

public function TuningMethod($tuning = null)
{
    // $tuning will contain the current tuning name, check
    $tuning_name = ($tuning);
    $tuning_name = strtoupper($tuning_name);
    $user = DB::table('guitar_tunings_links')->where('tuning', $tuning_name)->first();

    echo "<div class=\"panel panel-default\">
      <div class=\"panel-heading\">$user->name - $tuning_name</div>
      <div class=\"panel-body\">
        $user->description
      </div>
    </div>";

}

Here are my routes...

Route::any('{field}', 'TuningController@TuningMethod');

App::make('TuningController')->registerTuningRoutes();

This controller and routing gives me URLs like this laravel.dev/eadgbe which also returns all of the data about the guitar tuning, but it is not residing in my templating system. No styles or layouts are being used.

I have a blade template here app/views/home/tuning.blade.php

My controller works great. It takes the name of the Guitar Tuning from the DB and creates a route from it and prints out the required data from that row.

My problem is I cannot get the output of the controller and the routes to become part of the tuning.blade.php template.

I have tried adding these following routes

// Route 1
Route::get('tuning/{field}', function(){
    return View::make('home.tuning.{field}')
});

// Route 2
Route::get('tuning/{field}', function(){
    return View::make('home.{field}')
});

I have read extensively about routing controllers on the Laravel docs section, but I feel I am not understanding it or I am missing something simple.

Also I found this article on advance routing http://daylerees.com/codebright/advanced-routing and have tried to understand and implement it to no avail.

Can anyone help me understand how to to go about fixing this?

Was it helpful?

Solution

You don't really have to register all your routes the way you're doing because they can be resolved as only one route. This command was doing nothing, as far as I can tell:

Route::any($url, $route_name);

Because it's not pointing to a controller, it's just pointing to a name tuning.something, which is nothing to the Laravel router system. So, in fact your whole registerTuningRoutes function was doing nothing. But you already had a working route, which was doing all the work for:

Route::any('{field}', 'TuningController@TuningMethod');

That being said, looks like you can cut some of your code and just have keep this route:

Route::any('{field}', 'TuningController@TuningMethod');

Your controller method which is already working but would have to change to render the view directly:

<?php

public function TuningMethod($tuning = null)
{
    // $tuning will contain the current tuning name, check
    $tuning_name = ($tuning);
    $tuning_name = strtoupper($tuning_name);
    $user = DB::table('guitar_tunings_links')->where('tuning', $tuning_name)->first();

    return View::make('home.tuning')->with('user', $user)->with('tuning_name', $tuning_name);
}

The app/views/home/tuning.blade.php file could be something like:

@extends('layout')

@section('content')
    <div>
        You HTML here to beautifully render your page.

        This is your tunning details:
        <div class="panel panel-default">
            <div class="panel-heading">$user->name - $tuning_name</div>
            <div class="panel-body">
                $user->description
            </div>
        </div>
    </div>
@stop

And you should have a app/views/layout.blade.php file, to wrap it all with a HTML and body tags:

<html>
<body>
    @yield('content')
</body>
</html>

OTHER TIPS

I saw what you tried in the last part of your code (producing dynamic routes). I was really excited to test it on my side and tried to have the same routes but it did not work. I made the following changes to get it working. I hope these changes would be of any help.

P.S. Please pardon me if I got your question wrongly.

// Routes.php
Route::get('testCall/{testVariable}', array(
    'as' => 'test', // This is the name of your route
   'uses' => 'Parekhchintan30\Test\TestController@testFunction'
 )); 
// TestController.php
public function testFunction($testVariable){
return View::make('test::'.$testVariable); // obviously you will need a view with that name
}//test is the name of my package in this case
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top