PHP7框架之Lumen之Hello World,PHP7下的Lumen (5.4.5)框架之Hello World,如何调用Models下面的数据层,触目了解其MVC思想和取数据成功实践于基于其领悟,学下先进。 不指定

justwinit 2017-1-13 15:20 | |
背景:听说Lumen起家是以一个叫av的框架,larvael。以Auth2.0起步,Lumen是精简版本,于是想了解一下。发现居然边TM路由就卡住了,Fuck,不光是我老外也有这个问题,都没有解决,主要是一个Nginx的转写问题,这块在Lumen的文档里写的很垃圾,我都不知这些人是怎么写文档的,有用Apache,有Nginx的就不能写细一点么,说一下原理啥的,关键是Nginx配置文件里面的:location /里面的重写。
From:http://blog.csdn.net/wowkk/article/details/52104689
路由:https://lumen.laravel.com/docs/5.4
中文文档:http://laravelacademy.org/laravel-docs-5_4
5.4这个路由,老外写得很简单,这帮人可能是想你去看他的代码吧,有很多人在问:
http://www.thinksaas.cn/ask/question/22000/



/usr/local/composer/composer.phar  global require "laravel/lumen-installer"
https://lumen.laravel.com/docs/5.4#server-requirements 下面的:
chmod a+x /root/.config/composer/vendor/laravel/lumen-installer/lumen
/root/.config/composer/vendor/bin/lumen new lumen.levoo.com

/data/htdocs/lumen.levoo.com/blog/routes/web.php

你可以将所有路由都定义在routes/web.php中。最基本的 Lumen 路由接收:
http://laravelacademy.org/post/6337.html



$app->group(['prefix'=>'test'],function($app){
    $app->get("/index",["uses" => "TestController@index"]);
                                                                                                                                                                                                              
});


【lumen】基础点记录使用:blog.csdn.net/imdingding/article/details/48679359
app/Http/routes.php中进行路由配置可以使用如下路由组的形式,在路由上lumen和laravel有一些区别
$app->group(['prefix' => 'scrollnews','namespace' => 'App\Http\Controllers'],function($app) {
$app->get('/','NewsController@showall');
$app->get('/{time}_{newpage}.htm','NewsController@showtime');
$app->get('/{newpage}.htm', 'NewsController@show');

});


http://123.57.252.183/test/index
hello Lumen (5.4.5) (Laravel Components 5.4.*)
/data/htdocs/lumen.levoo.com/app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use App\user;   //新增部分
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class TestController extends BaseController
{
    //直接传人sql方式操作数据库
    function index(Request $request){
        echo "hello Lumen (5.4.5) (Laravel Components 5.4.*)。";die;                                                                                                                                          
        return User::all();
    }
}


以上实践来源及变通参看了:http://blog.5ibc.net/p/87952.html

PHP message: PHP Fatal error:  Uncaught UnexpectedValueException: The stream or file "/data/htdocs/lumen.levoo.com/storage/logs/lumen.log" could not be opened: failed to open stream: Permission denied in /data/htdocs/lumen.levoo.com/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:107
mkdir
chmod -R 755 /data/htdocs/lumen.levoo.com/storage
chown -R www /data/htdocs/lumen.levoo.com/storage


Controller类的写法:https://segmentfault.com/q/1010000002727820
/data/htdocs/lumen.levoo.com/blog/app/Http/Controllers/AccountController.php



关于数据库配置文件:
默认情况下, Lumen 使用单一的 .env 文件来配置你的应用, 然而, 你也可以使用 Laravel 风格 的配置方法.
/data/htdocs/lumen.levoo.com/vendor/laravel/lumen-framework/config/database.php
文件夹下对应的配置文件复制到根目录下的 config 文件里面就行.
https://lumen.laravel-china.org/docs/5.3/configuration#configuration-files

Redis:
CACHE_DRIVER=file
SESSION_DRIVER=redis
QUEUE_DRIVER=sync

REDIS_HOST=192.168.1.248
REDIS_PASSWORD=null
REDIS_PORT=6379



今天在部署服务器的时候,使用composer来安装依赖。遇到了如下情况,这块出错好像和Redis配置有关,得在.env里配置好并运行起来。
  The Process class relies on proc_open, which is not available on your PHP installation.
  [ErrorException]                                          
  proc_get_status() has been disabled for security reasons
  开始的时候,我有些不知所措,于是冷静下来思考一番,然后goolge了一下
解决方法:
  在php.ini中,找到disable_functions选项,看看后面是否有proc_open函数被禁用了,如果有的话,去掉即可
  其实如果php的文档熟悉的话,你应该马上就能知道proc_open实际上是一个函数,是php用来和shell交互的函数,一般这种可以直接作用于操作系统的函数是非常不安全的,对于这种不安全的函数,总是要做一些措施来进行保护

php artisan make:migration create_table_cars --create=cars
php artisan make:migration create_table_cars --create=cars
Created Migration: 2017_03_04_160644_create_table_cars
/data/htdocs/lumen.levoo.com/database/migrations/2017_03_04_160644_create_table_cars.php

    public function up()
    {
        Schema::create('cars', function (Blueprint $table) {
            $table->increments('id');
            $table->string('make');                                                                                                                                  
            $table->string('model');
            $table->string('year');
            $table->timestamps();
        });
    }



php artisan migrate
Migrated: 2017_03_04_161708_create_table_cars

migrations记录历史 /car 两个表:
id int(10) UNSIGNED
make  varchar(255)
model varchar(255)
year varchar(255)
create d_attimestamp


问题实践来源:http://www.cnblogs.com/crisenchou/p/6237781.html
打开方法来自:http://www.codesec.net/view/497671.html?winzoom=1



Model这块和Controll层的配置:
1)Controll层, /data/htdocs/lumen.levoo.com/app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;
use App\Models\Information;
use App\Models\Collection;

//use App\User;     //新增部分APP目录下
use App\Models\User; //App\Models\User目录下
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class TestController extends BaseController
{
    //直接传人sql方式操作数据库
    function index(Request $request){
        /*
            $UserModel = new User();
            $result = $User::all(); //这样也成
            $result2 = $UserModel::all2(); //这样也成
        */
        $result = User::all();
        $result2 = User::all2();//这个在Model自定义的all2也能被调用输出结果

        print_r($result);
        echo "<hr>";                                                                                                                                                                                          
        print_r($result2);
    }
}


2)Model层:/data/htdocs/lumen.levoo.com/app/Models/User.php

<?php
//namespace App; //这个得注释掉,容易和App/User.php发生错误引用。
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
//默认操作数据库的users表,需要定制的话请看文档,下面有自己的配置。
class User extends Model
{
    /**
    * The connection name for the model.
    *
    * @var string
    */
    protected $connection = 'mysql';

    /**
    * 表名
    *
    * @var string
    */
    protected $table = 'levoo_sns_user'; //指定操作表名
    protected $primaryKey = "id";        //指定主键
    public $timestamps = false;
    protected $dateFormat = 'U';

    static public function all2(){
        $User = User::all();
        return $User;                                                                                                                                                                                          
    }
}


=================MVC模板学习=============================
/data/htdocs/lumen.levoo.com/app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;
use App\Models\Information;
use App\Models\Collection;

//use App\User;     //新增部分APP目录下
use App\Models\User; //App\Models\User目录下
use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class TestController extends BaseController
{
    //直接传人sql方式操作数据库
    function index(Request $request){
        /*
            $UserModel = new User();
            $result = $User::all(); //这样也成
            $result2 = $UserModel::all2(); //这样也成
        */
        $result = User::all();
        //$result2 = User::all2();//这个在Model自定义的all2也能被调用输出结果
        /*
        print_r($result);                                                                                                                                            
        echo "<hr>";
        print_r($result2);
        */
        //view()->exists('user');
        return view('test', ['name' => 'jackX','results' => $result]);
    }
}


vi /data/htdocs/lumen.levoo.com/resources/views/test.blade.php


<!-- 该视图存放 resources/views/greeting.php -->

<html>
    <body>
        <h1>Hello, {{ $name }}</h1>
        <h1>Hello, {{ $name2 or 'Default' }}</h1>
        <h1>Hello, {!! $name !!}</h1>
        <h1>Array, {{ $results}}</h1>
        <h1>Array, {!! $results !!}</h1>
        Blade for each:<br>
        <h1>
            @foreach ($results as $user)
                <ul>
                <li>This is user {{ $user->id }}</li>
                <li>This is user {{ $user->mobile }}</li>
                <li>This is user {{ $user->fansmobile }}</li>
                <li>This is user {{ $user->followedTime }}</li>
                </ul>
            @endforeach
        </h1>
        <h1>
            @foreach ($results as $user)
                <ul>
                @if ($user->id == 1)
                   用户Id=1,标识一下:
                @endif
                <li>This is user {{ $user->id }}</li>
                <li>This is user {{ $user->mobile }}</li>
                <li>This is user {{ $user->fansmobile }}</li>
                <li>This is user {{ $user->followedTime }}</li>
                </ul>
            @endforeach
        </h1>
        <h1>只显示id=1的区块:</h1>
        <h1>
            @foreach ($results as $user)
                <ul>
                 @continue($user->id == 2)
                <li>This is user {{ $user->id }}</li>
                <li>This is user {{ $user->mobile }}</li>
                <li>This is user {{ $user->fansmobile }}</li>
                <li>This is user {{ $user->followedTime }}</li>
                </ul>
                 @break($user->id == 1)
            @endforeach
        </h1>
    </body>
</html>




模板输出:http://123.57.252.183/test/index
Hello, jackX

Hello, Default

Hello, jackX

Array, [{"id":1,"mobile":"1881**65108","fansmobile":"18810322234","followedTime":1211212121},{"id":2,"mobile":"1881**65108","fansmobile":"18810322232","followedTime":121212}]

Array, [{"id":1,"mobile":"1881**65108","fansmobile":"18810322234","followedTime":1211212121},{"id":2,"mobile":"1881**65108","fansmobile":"18810322232","followedTime":121212}]

Blade for each:
This is user 1
This is user 1881**65108
This is user 18810322234
This is user 1211212121
This is user 2
This is user 1881**65108
This is user 18810322232
This is user 121212
用户Id=1,标识一下:
This is user 1
This is user 1881**65108
This is user 18810322234
This is user 1211212121
This is user 2
This is user 1881**65108
This is user 18810322232
This is user 121212
只显示id=1的区块:

This is user 1
This is user 1881**65108
This is user 18810322234
This is user 1211212121

这个叫Blade的模板引擎解析成PHP的,也就是相当于没有啥大的损耗,如果它的算法还算先进的前提下,位置在:
/data/htdocs/lumen.levoo.com/storage/framework/views/9e3b647aebb6a248d1ae42408e87c3fc7f5bc520.php
为什么这么说呢,如下,我在想为何要搞成这样,PHP裸写不也一样么,像Ci框架啥的,思考思考,这样也算是能模板和程序分离罢:


        <h1>只显示id=1的区块:</h1>
        <h1>
            <?php $__currentLoopData = $results; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $user): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                <ul>
                 <?php if($user->id == 2) continue; ?>
                <li>This is user <?php echo e($user->id); ?></li>
                <li>This is user <?php echo e($user->mobile); ?></li>
                <li>This is user <?php echo e($user->fansmobile); ?></li>
                <li>This is user <?php echo e($user->followedTime); ?></li>
                </ul>
                 <?php if($user->id == 1) break; ?>
            <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
        </h1>



主要是对这个模板有一个大体理解,和Smarty啥的有一个初步了解,参考自:
http://laravelacademy.org/post/6780.html




======================================================================
Nginx配置文件:

server
{
    listen       80 default_server;
    server_name  101.200.189.210 lumen.levoo.com;
    index index.html index.htm index.php;
    root  /data/htdocs/lumen.levoo.com/blog/public;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;


    location ~ .*\.(php|php5)?$
    {
        fastcgi_pass unix:/dev/shm/php-fcgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        #if (!-f $request_filename){                                                                                                  
        #    rewrite ^/(.+)$ /index.php?s=$1 last;
        #    break;
        #}
    }
    access_log /data/logs/nginx/access_lumen_levoo.log;
}



上面location / {里被注释掉的也成,只是优雅度不够,这块好像手册有说明,我当时把 try_files $uri $uri/ /index.php?$query_string;放到location ~ .*\.(php|php5)?$ 里了,发现不对。

http://123.57.252.183/foo
Hello World


老外:
http://stackoverflow.com/questions/32281294/lumen-simple-route-request-doesnt-work
老外也有这个问题:
http://laravel.io/forum/05-08-2014-nginx-laravel-routing-shows-404?page=1

主从数据库配置(多台读库配置):
http://blog.sina.com.cn/s/blog_9bbafb790102win1.html
基于Lumen搭建一个OAUTH2认证的API框架:
http://www.bibihub.com/php/lumen-mobile-api-oauth-2-authentication/

========DB学习=========
基本使用
注意:如果你想要使用DB门面,应该取消bootstrap/app.php文件中$app->withFacades()调用前的注释
vi bootstrap/app.php

下面这个需要门面:

$app->get('/foo', function () use ($app) {
    $results = app('db')->select("SELECT * FROM levoo_sns_user");
    print_r($results);
    $results = DB::select("SELECT * FROM levoo_sns_user");//这个需要门面
    print_r($results);
    return 'Hello World';                                          
});

作者:justwinit@向东博客 专注WEB应用 构架之美 --- 构架之美,在于尽态极妍 | 应用之美,在于药到病除
地址:http://justwinit.cn/post/9177/
版权所有。转载时必须以链接形式注明作者和原始出处及本声明!


最后编辑: justwinit 编辑于2017-6-24 13:14
评论列表
发表评论

昵称

网址

电邮

打开HTML 打开UBB 打开表情 隐藏 记住我 [登入] [注册]