2016-01-08 59 views
1

关于如何转换此Laravel-5.1代码的任何想法。我有这个代码在纯PHP中运行,因为我正在使用Laravel进行开发,所以我想写在Laravel中。将我指向已经完成此操作或提供代码段的教程将受到高度赞赏。以Laravel-5.1格式重写纯PHP代码

<?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> 
+0

请阅读:https://laravel.com/docs/5.1/requests –

+0

由于您已经在使用Laravel进行开发,因此应该很简单:创建新的控制器来处理请求,创建geonames_names模型以从中获取数据数据库,返回JSON响应。你正在努力与哪些? –

+0

http://stackoverflow.com/questions/34679066/using-bootstrap-typeahead-in-laravel-5-1 @Alex Blex我试过实现,但没有运行。请,我已经发布了我上面的执行链接。尽管json已经返回,但我认为这是不可行的。 – kehinde

回答

1

那么既然你问了Laravel的方式来做事会有来完成这件事只有几步之遥。简而言之,您需要1)创建模型,2)更新您的routes.php文件,3)创建控制器,以及(4)更新您的ajax调用以反映Laravel的路由约定。我建议使用命令行php artisan命令创建模型和控制器,因为它们会将必要的文件放在正确的路径中,以便Laravel将自动为您加载它们。

  1. 模式 - 在命令行中运行php artisan make:model GeoName,这应该在app/GeoName.php中,你将需要更改表名,以反映您custome名称创建一个模型。

    <? 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发布请求。

    Route::post('bootstrap/source','[email protected]');

    这将赶上POST请求http://localhost:2222/bootstrap/cache,还有更多在这里Laravel Documents on routes

  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请求中删除请求调用中的.phpurl: 'http://localhost:2222/bootstrap/source',因为您永远不会直接在Laravel中调用文件,路径文件会处理您的应用程序的所有目标。

值得注意的有几件事情,(1)你的数据库应该使用.envand app/config.php文件进行配置,(2)Laravel会自动检测到jQuery的AJAX函数需要一个JSON响应,以便Laravel将提供什么它要求。

您可能会遇到XSFR令牌权限问题,您可以在这里阅读有关如何解决Laravel Docs问题的问题。如果你还不知道,Laravel's Master Documentation非常棒!

当然还有很多要学习使用Laravel和许多优秀的Laravel资源。祝你好运!