简体   繁体   English

Laravel路由动态路由控制器到刀片模板

[英]Laravel route dynamic routes controller to blade template

How do I make the controller display in my blade layouts? 如何使控制器显示在刀片​​布局中?

I have 250 dynamic routes imported from MySQL. 我有从MySQL导入的250条动态路由。 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. 该控制器和路由为我提供了类似laravel.dev/eadgbe这样的URL,该URL还返回了有关吉他调音的所有数据,但它不存在于我的模板系统中。 No styles or layouts are being used. 没有使用任何样式或布局。

I have a blade template here app/views/home/tuning.blade.php 我在这里有一个刀片模板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. 我的问题是我无法获得控制器的输出和路由,无法成为tuning.blade.php模板的一部分。

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. 我已经在Laravel docs部分上广泛阅读了有关路由控制器的内容,但是我感觉我不太了解它,或者我缺少一些简单的东西。

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. 另外,我在http://daylerees.com/codebright/advanced-routing上找到了有关高级路由的这篇文章,并试图理解并实现它毫无用处。

Can anyone help me understand how to to go about fixing this? 谁能帮助我了解如何解决此问题?

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. 因为它不指向控制器,所以仅指向名称tuning.something ,这对Laravel路由器系统而言毫无意义。 So, in fact your whole registerTuningRoutes function was doing nothing. 因此,实际上您的整个registerTuningRoutes函数什么都不做。 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: app/views/home/tuning.blade.php文件可能类似于:

@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: 并且您应该有一个app/views/layout.blade.php文件,用HTML和body标签将其包装起来:

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

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. 我希望这些更改会有所帮助。

PS 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM