【问题标题】:Laravel dynamic route from MySQL来自 MySQL 的 Laravel 动态路由
【发布时间】:2014-04-20 23:54:29
【问题描述】:

我正在尝试从一个模板“tuning.blade.php”生成路由

我有一个有 250 行的数据库,我想用一个路由或一个控制器动态创建 250 个路由。

我希望能够使用这些网址

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

我想把 DB 请求放在tuning.blade.php 中,这样这个模板就可以使用 250 个不同的 URL 显示所有 250 行

我尝试使用 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');

我还从http://forumsarchive.laravel.io/viewtopic.php?id=9010 得到了一些有趣的搜索结果 但是我总是以未找到的异常结束

但我不确定在我的调整模板中添加什么来显示任何内容。我的调优模板位于 app/views/home/tuning.blade.php

目前我收到错误“Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException”

任何人都可以让我找到正确的方向来帮助我理解吗?

【问题讨论】:

    标签: dynamic laravel blade laravel-routing


    【解决方案1】:

    你说过你希望能够使用这些URLs:

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

    您可以通过声明这样的路线来做到这一点:

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

    您不应该从您的view 运行 sql 查询,如果您想真正为数据库中的每个字段声明一些动态路由,那么您可以直接从您的 routes.php 文件中执行 id,例如,假设您有一个名为tunings 的表,该表包含一些字段,包括idname 和其他一些字段。现在,要使用表tuningstuning 字段为每个路由动态单独声明路由,您可以在TuningController 中创建一个方法,如下所示:

    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);
        }
    }
    

    现在在您的 routes.php 文件中使用如下内容:

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

    从您的终端/命令提示符通过运行以下命令检查路线:

    php artisan routes
    

    但是,我认为,您不需要这样做,正如我之前在回答中提到的那样,只需一条路线就足够了。

    【讨论】:

    • 感谢您的宝贵时间。我不确定我是否完全理解我将如何使用它。我添加了 TuningController 并发出了模型,因为它引发了错误。我添加了 App::make('TuningController')->registerTuningRoutes();到我的路由文件,但在浏览器中出现未找到的异常。虽然当我运行 php artisan routes 时,我可以在那里看到它们!如何查看每条路线?
    • 当您使用php artisan routes 时,请尝试对控制台中提供的每个路由使用url,并在更新的答案中检查TuningMethod
    • 再次感谢 :) 我创建了一个 url,即 laravel/tuning/cgcfce,它打印了 string 'cgcfce' (length=6),但如果我将 cgcfce 更改为不在数据库中的东西,它也会打印出来,即 string 'cgcfce' (length=6)那正确吗?理想情况下,我想打印出行和样式的内容,并使用 php 和 css 进行更改
    • 对不起,我的意思是它会打印出数据库中没有的东西,即字符串'cgcfceX' (length=7)
    • 感谢您抽出宝贵的时间。我现在意识到我在 TuningMethod 函数内部执行了所有逻辑。非常感谢你让我明白这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多