PHP code example of zhuitech / boot-laravel

1. Go to this page and download the library: Download zhuitech/boot-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zhuitech / boot-laravel example snippets



// app/Providers/AppServiceProviders.php
namespace App\Providers;

use ZhuiTech\BootLaravel\Providers\LaravelProvider;
use ZhuiTech\BootLaravel\Providers\AbstractServiceProvider;
use ZhuiTech\BootLaravel\Providers\MicroServiceProvider;

// 基类ServiceProvider中提供了很多方便的注册机制,请查看源码了解
class AppServiceProvider extends AbstractServiceProvider
{
    protected $providers = [
        LaravelProvider::class,
        // 如果是微服务,需要添加MicroServiceProvider
        MicroServiceProvider::class,
    ];
}


// database/migrations/2018_04_04_122200_create_channels_table.php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateChannelsTable extends Migration
{
    public function up()
    {
        Schema::create('channels', function (Blueprint $table) {
            $table->increments('id');
            // 添加需要的字段
            $table->string('name')->comment('名称');
            // ...
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('channels');
    }
}


// app/Repositories/ChannelRepository.php
namespace App\Repositories;

use App\Models\Channel;
use ZhuiTech\BootLaravel\Repositories\BaseRepository;

// 基类BaseRepository提供了很多现成的方法,请查看源码了解
class ChannelRepository extends BaseRepository
{
    function model()
    {
        return Channel::class;
    }
}


// app/Http/Controllers/ChannelController.php
namespace App\Http\Controllers;

use App\Repositories\ChannelRepository;
use ZhuiTech\BootLaravel\Controllers\RestController;

// 基类ChannelController提供了很多现成的方法,请查看源码了解
class ChannelController extends RestController
{
    public function __construct(ChannelRepository $repository)
    {
        parent::__construct($repository);
    }
    
    public function sample()
    {
        // 用以下方法会返回统一的数据格式
        $this->success([]);
        $this->fail('fail sample message');
    }
}

// routes/api.php
Route::group(['prefix' => 'mail',], function () {
    // Channels
    Route::resource('channels','ChannelController');
});


namespace App\Http\Controllers;

use App\Repositories\MessageRepository;
use App\Repositories\SendingRepository;
use ZhuiTech\BootLaravel\Controllers\SubRestController;

// 基类SubRestController提供了很多现成的方法,请查看源码了解
class SendingController extends SubRestController
{
    // 这里同时注入Parent和Client的仓库类
    public function __construct(SendingRepository $repository, MessageRepository $parents)
    {
        parent::__construct($repository, $parents);
    }
}

Route::group(['prefix' => 'messages',], function () {
    Route::resource('{message}/sendings', 'SendingController');
});

// 请求后端服务
$result = RestClient::server('mail')->get('api/mail/channels');

// 处理返回结果
return Restful::handle($result,
    function($data, $message, $code) {
        // success
    },
    function($data, $message, $code) {
        // fail
    });

// Channels
Route::resource('channels', 'ChannelController')->middleware(['auth:admins']);
Route::resource('channels', 'ChannelController')->middleware(['auth:members']);

$user = Auth::user();
if (empty($user)) {
    // 未登录
}
else {
    // 已登录
    $admin = $user->isAdmin();
    $member = $user->isMember();
    $id = $user->id;
    $type = $user->type;
    $ip = $user->ip;
    $user->loadData(); // 加载完整用户数据
}

// 读取配置
$value = setting('key');

// 更新配置,$value可以是一个数组
setting(['key' => $value]);
bash
# 更新Facade等提示
php artisan ide-helper:generate
# 更新容器内对象提示
php artisan ide-helper:meta
# 更新模型类提示
php artisan ide-helper:models -W -R
# 发布前端资源
php artisan vendor:publish --force --tag=public
bash
# app/Models/Channel.php
php artisan make:model -c -m Models/Channel
bash
# 生成对应的表
php artisan migrate
# 生成模型提示
php artisan ide-helper:models -W -R