在Laravel-5.1格式重新编写纯PHP code [英] Re-write pure PHP code in Laravel-5.1 format

查看:162
本文介绍了在Laravel-5.1格式重新编写纯PHP code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是如何将这种Laravel 5.1 code转换任何想法。我在纯PHP这个code跑步,我会喜欢,因为我使用Laravel的发展,它写在Laravel。指着我对已完成本教程或提供code段将是非常美联社preciated。

 < PHP
    如果(使用isset($ _ POST ['查询'])){        mysql_connect('localhost'的,'根','');
        mysql_select_db('tradersmart');
        $查询= $ _ POST ['查询'];
        $ SQL =的mysql_query(选择名称,从timezone_id WHERE geonames_names名LIKE'%{$查询}%');
        $ arrayName中=阵列();
        $ arrayTimezone =阵列();
        而($行= mysql_fetch_assoc($ sql中)){
            $ arrayName中的[] = $行['timezone_id'];
            $ arrayTimezone [] = $行[名称];
        }
        回声json_en code($ arrayName中的+ $ arrayTimezone);
    }
?>

这是HTML文件:它使用JSON和预输入,加快建议

 <身体GT;
        < D​​IV CLASS =以及>
            <输入类型=文本级=CSS-输入ID =预输入数据提供者=预输入>
        < / DIV>
        &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js>&下; /脚本>
        <脚本类型=文/ JavaScript的SRC =JS / bootstrap.js>< / SCRIPT>        <脚本>
            $(函数(){
                $('#预输入')预输入({
                    来源:功能(查询,处理){
                        $阿贾克斯({
                            网址:的http://本地主机:2222 /引导/ source.php',
                            输入:POST,
                            数据:'查询='+查询,
                            数据类型:JSON,
                            异步:真实,
                            成功:功能(数据){
                                处理数据);
                            }
                        });
                    }
                });
            });
        < / SCRIPT>
    < /身体GT;


解决方案

那么既然你问了Laravel的方式做事情会有几个步骤来完成这件事。总之,你需要1)建立一个模型,2)更新 routes.php文件文件,3)创建一个控制器,以及(4)更新您的Ajax调用,以反映Laravel的路由约定。我建议使用命令行 PHP工匠命令来创建模型和控制器,因为他们将放置在正确的路径所需的文件,以便Laravel会自动加载它们。


  1. 示范 - 运行 PHP工匠化妆:型号GeoName 在命令行中,这应该在创建一个模型应用程序/ GeoName.php 中,你将需要更改表名,以反映您custome名。

     &LT ;?空间应用;使用照亮\\数据库\\锋\\型号;类GeoName扩展模式
    {//这将映射您的自定义表名laravel。
    保护$表=geonames_names;}

    Laravel会自动期望表名是在这种情况下,模型的复数版本,它会寻找 GEONAMES ,重写,你需要添加保护$表属性之上。


  2. 更​​新应用程序/ HTTP / routes.php文件文件搭上AJAX POST请求。

    路线::后(引导/源','GeoNameController @ ajaxQuery');

    这将搭上 POST 要求的http://本地主机:2222 /引导/缓存,还有更多在航线上Laravel 的文件在这里。


  3. 创建使用 PHP工匠化妆控制器:在命令行控制器GeoNameController --plain 。平原在这里用来停止典型的CRUD请求类型指数的自动脚手架,创建,编辑,显示,更新和删除。这将创建文件应用程序/ HTTP /控制器/ GeoNameController.php

     < PHP空间应用\\ HTTP \\控制器;使用照亮\\ HTTP \\请求;使用应用程序\\ HTTP \\请求;
    使用应用程序\\ HTTP \\控制器\\控制器;类GeoNameControler扩展控制器
    {
         //添加以下几条     公共职能ajaxQuery(请求$要求){         $查询= $请求 - >获取('查询');         $ GEONAMES = GeoName ::其中('名',$查询) - GT;清单('名','timezone_id') - GT;获得();         返回$ GEONAMES;
         }
    }

    请记住,查询 $查询= $请求 - &GT被使用;获得('查询'); ,因为那是你命名你的Ajax请求数据变量。 (数据:'查询='+查询,


  4. 最后,在jQuery的Ajax请求的请求调用删除的.php 网​​址:的http://本地主机:2222 /引导/源',你将永远不会直接调用在Laravel文件,路由文件处理所有目的地为您的应用。


请注意的几件事情,(1)您的数据库应该使用 .ENV 和应用程序/ config.php文件配置文件,(2)Laravel会自动检测到jQuery的AJAX功能期待一个JSON响应,以便Laravel将提供正是它要求。

您可能会遇到与XSFR令牌权限问题的问题,你可以了解如何在的 Laravel文档这里。如果你不已经知道, Laravel的主文档是优秀的!

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

any ideas on how to convert this Laravel-5.1 code. I have this code running in pure PHP, I'll like to write it in Laravel since I'm using Laravel for the development. Pointing me to a tutorial that has already done this or providing a code snippet would be highly appreciated.

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

THis is the HTML file: It uses JSon and typeahead to speed up suggestion.

<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>

解决方案

Well since you are asking for the Laravel way to do things there will be few steps to get this done. In short, you need to 1) create a model, 2) update your routes.php file, 3) create a controller, and (4) update your ajax call to reflect Laravel's routing conventions. I suggest using the command line php artisan commands to create the Model and Controller as they will place the necessary files in the correct paths so that Laravel will autoload them for you.

  1. Model - run php artisan make:model GeoName from the command line, this should create a model at app/GeoName.php in which you will need to change the tablename to reflect your custome name.

    <? 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 will automatically expect the tablename to be a plural version of the model in this case it would look for geonames, to override that you'll need add the protected $table attribute above.

  2. Update the app/Http/routes.php file to catch the AJAX post request.

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

    This will catch a POST request to http://localhost:2222/bootstrap/cache, there's more in the Laravel Documents on routes here.

  3. Create a controller using php artisan make:Controller GeoNameController --plain in the command line. Plain was used here to stop the automatic scaffolding of the typical CRUD request types of index, create, edit, show, update and delete. This will create the file 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;
         }
    }
    

    Keep in mind that query is used in $query = $request->get('query'); because that is what you named the data variable in your ajax request. ( data: 'query=' +query,)

  4. Finally, in your jQuery ajax request remove the .php in the request call. url: 'http://localhost:2222/bootstrap/source' as you'll never directly call files in Laravel, the routes files handles all the destinations for your application.

A few things of note, (1) Your database should be configured using the .env and app/config.php files, (2) Laravel will automatically detect that the jQuery ajax function is expecting a JSON response so Laravel will provide exactly what it asks for.

You will may likely run into an issue with XSFR token permission issue which you can read about how to resolve in the Laravel Docs here. If you don't already know, the Laravel's Master Documentation is excellent!

Of course there is a lot more to learn about using Laravel and many excellent Laravel resources out there. Good luck!

这篇关于在Laravel-5.1格式重新编写纯PHP code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆