以 Laravel 5.1 格式重新编写纯 PHP 代码

问题描述 投票:0回答:1

我有这个代码在纯 PHP 中运行,我想在 Laravel 5.1 中编写它,因为我使用 Laravel 进行开发。

<?php
    if(isset($_POST['query'])){
    
        mysql_connect('localhost', 'root', '');
        mysql_select_db('tradersmart');
        $query = $_POST['query'];
        $sql = mysql_query("SELECT name, timezone_id FROM geonames_names WHERE name LIKE '%{$query}%'");
        $arrayName = array();
        $arrayTimezone = array();
        while($row = mysql_fetch_assoc($sql)){
            $arrayName[] = $row['timezone_id'];
            $arrayTimezone[] = $row['name'];
        }
        echo json_encode($arrayName  +$arrayTimezone);
    }
?>

这是 HTML 文件:它使用 JSON 和 typeahead 来加快建议速度。

<body>
        <div class="well">
            <input type="text" class="css-input" id="typeahead" data-provider="typeahead">
        </div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script type="text/javascript" src="js/bootstrap.js"></script>
        
        <script>
            $(function(){
                $('#typeahead').typeahead({
                    source: function(query, process){
                        $.ajax({
                            url: 'http://localhost:2222/bootstrap/source.php',
                            type: 'POST',
                            data: 'query=' +query,
                            dataType: 'JSON',
                            async: true,
                            success: function(data){
                                process(data);
                            }
                        });
                    }
                });
            });
        </script>
    </body>
php arrays json html bootstrap-typeahead
1个回答
1
投票

既然你要求用 Laravel 的方式来做事,那么只需几个步骤就可以完成这件事。简而言之,您需要 1) 创建一个模型,2) 更新您的

routes.php
文件,3) 创建一个控制器,以及 (4) 更新您的 ajax 调用以反映 Laravel 的路由约定。我建议使用命令行
php artisan
命令来创建模型和控制器,因为它们会将必要的文件放置在正确的路径中,以便 Laravel 会为您自动加载它们。

  1. 模型 - 从命令行运行

    php artisan make:model GeoName
    ,这应该在
    app/GeoName.php
    创建一个模型,您需要在其中更改表名以反映您的客户名称。

    <? namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class GeoName extends Model
    {
    
    // this will map your custom table name to laravel. 
    protected $table = "geonames_names"; 
    
    }
    

    Laravel 会自动期望表名是模型的复数版本,在这种情况下它将查找

    geonames
    ,要覆盖您需要添加上面的
    protected $table
    属性。

  2. 更新

    app/Http/routes.php
    文件以捕获 AJAX post 请求。

    Route::post('bootstrap/source','GeoNameController@ajaxQuery');

    这将捕获对

    POST
    http://localhost:2222/bootstrap/cache
    请求,这里的 Laravel 路线文档中有更多内容。

  3. 在命令行中使用

    php artisan make:Controller GeoNameController --plain
    创建控制器。这里使用 Plain 来停止索引、创建、编辑、显示、更新和删除等典型 CRUD 请求类型的自动搭建。这将创建文件
    app/Http/Controllers/GeoNameController.php

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    use App\Http\Requests;
    use App\Http\Controllers\Controller;
    
    class GeoNameControler extends Controller
    {
         // Add the following here
    
         public function ajaxQuery(Request $request){
    
             $query = $request->get('query');
    
             $geoNames = GeoName::where('name',$query)->lists('name','timezone_id')->get();
    
             return $geoNames;
         }
    }
    

    请记住,

    query
    用于
    $query = $request->get('query');
    中,因为这是您在ajax请求中命名数据变量的名称。 (
    data: 'query=' +query,
    )

  4. 最后,在 jQuery ajax 请求中删除请求调用中的

    .php
    url: 'http://localhost:2222/bootstrap/source'
    因为你永远不会直接在 Laravel 中调用文件,所以路由文件会处理你的应用程序的所有目的地。

需要注意的一些事项,(1) 您的数据库应使用

.env
and app/config.php
文件进行配置,(2) Laravel 将自动检测 jQuery ajax 函数是否需要 JSON 响应,因此 Laravel 将准确提供其所要求的内容为了。

您可能会遇到 XSFR 令牌权限问题,您可以在此处的 Laravel 文档 中了解如何解决该问题。如果您还不知道,Laravel 的主文档非常棒!

当然,关于使用 Laravel 以及许多优秀的 Laravel 资源还有很多东西需要学习。祝你好运!

© www.soinside.com 2019 - 2024. All rights reserved.