Laravel Migrate修改表和创建表

php artisan make:migration create_table_test –table=test_a 修改表

Schema::table(‘test’, function (Blueprint $table) {
$table->dropColumn([‘data’]);
$table->renameColumn(‘tttt’,’test’);
$table->addColumn(‘string’,’t_id’, [‘length’ => 200]);
});

php artisan make:migration create_table_test –create=test_a 创建表
Schema::create(‘users’, function (Blueprint $table) {
$table->increments(‘id’);
$table->string(‘name’);
$table->string(’email’, 150)->unique();
$table->string(‘password’);
$table->rememberToken();
$table->timestamps();
});

 

需要添加:

{"require": {"doctrine/dbal": "v2.5.5"}}
Posted in Laravel | Leave a comment

Laravel middleware中间件

Laravel Middleware 中间件

php artisan make:middleware TestMiddleware

public function handle($request, Closure $next)
{
if($request->input(‘id’) > 1) {
die( ‘hello world’ );
}
return $next($request);
}
注册中间件
app/Http/Kernel.php$middleware添加

控制器当中
$this->middleware(‘Test’);
路由器
Route::get(‘/’,’Test\\TestController@index’, [‘middleware’ => ‘Test’]);

Posted in Laravel | Tagged , | Leave a comment

Laravle Job 案例

make:job 创建一个新JOb类

protected $msg;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($msg)
{
$this->msg = $msg;
}

/**
* Execute the job.
*
* @return void
*/
public function handle()
{
echo date(‘Y-m-d H:i:s’).”=>”.$this->msg .”\n”;
}
控制器使用案例
for($i = 0; $i < 10; $i ++) {
$job = new TestJob(‘hello ‘. $i);
$this->dispatch($job);
}

Posted in Laravel, PHP | Leave a comment

Laravel Command命令行

make:command php artisan make:command Test/Test生成新命令
在app/Console/Kernel.php文件当中注册命令
protected $commands = [
//
TestCommand::class,
];
编辑命令
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = ‘test:test {name} {date?} {default=default} {–op1=}’;
//{name} 必须参数 {date?} 非必须 {default=default}默认参数 {–op1}设置项
/**
* The console command description.
*
* @var string
*/
protected $description = ‘命令描述’;

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$argument = $this->argument();
print_r($argument);
echo $this->option(‘op1’);
$this->info(‘提示信息’);
}
执行命令
php artisan test:test name1 date1 default –op1=option1
//程序调用
Artisan::call(“test:test”, array(‘name’ => ‘test’, ‘date’ => ‘date1’, ‘default’ => ‘def’,’–op1′ => ‘option’));

Posted in Laravel, PHP | Tagged , | Leave a comment

Laravel Event和Listener

make:event TestEvent 创建一个新事件类
php artisan make:listener TestListener –event TestEvent 创建一个listener类
注册事件TestEvent
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($str)
{
$this->data = $str;
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return [];
}
监听时间TestListener
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param TestEvent $event
* @return void
*/
public function handle(TestEvent $event)
{
file_put_contents(‘/data/tmp/test.log’, $event->data, FILE_APPEND);
}
注册应用关联事件EventServiceProvider.php
protected $listen = [

‘app\Events\TestEvent’ => [
‘app\Listeners\TestListener’,
],
];
触发事件
$str = ‘test….’;
Event::fire(new TestEvent($str));
event(new TestEvent($str));

php artisan event:generate 批量生成event和listen 编辑EventServiceProvider.php

备注:事件可以广播,通过websocket服务,依赖Redis的pub和sub模式

Posted in Laravel, PHP | Tagged , , | Leave a comment