laravel5.1 邮箱的使用

  1. 配置env文件
    1. MAIL_DRIVER=smtp
    2. MAIL_HOST=smtp.163.com
    3. MAIL_PORT=25
    4. [email protected]
    5. MAIL_PASSWORD=这里是客户端授权密码
    6. MAIL_ENCRYPTION=null 这里如果是http就不用改,https就改为ssl

2.config目录下面 mail.php 里面

‘from’ => [‘address’ => ‘[email protected]’, ‘name’ => ‘bobo’],

3.发送测试就好了

Route::get(‘mail’, function () {

$data = [‘name’ => ‘CanoeChow’];

Mail::send(’email’, $data, function ($message) {

$message->to(‘[email protected]’)->subject(‘Welcome!’);

$message->attach(public_path().’img/pic.jpg’);

});

return ‘OK!’;

});

laravel5.1 封装第三方服务

App目录下新建Facades 1.

<?php namespace App\Facades;

use Illuminate\Support\Facades\Facade;

class QiniuServiceFacade extends Facade {

protected static function getFacadeAccessor()

{

return ‘qiniu’;

}

}

App下新建Services文件夹存放处理逻辑业务的类 QiniuService.php  2.

App下新建Providers 在register 里面写单利模式,参数1是facade的字符串,参数2是类的地址

config/app.php  里面

‘providers’下App\Providers\AppServiceProvider::class,

‘aliases’下’Qiniu’      => App\Facades\QiniuServiceFacade::class,

最后路由就可以使用静态了

Route::get(‘/test’, function(){

$token = Qiniu::getToken();

return view(‘test’)->with(‘token’, $token);

});

Route::post(‘/test’,function(Request $request){

$payload = $request->all();

$result = Qiniu::upLoad($payload[‘file’]);

return Qiniu::getDomain().’/’.$result[‘key’];

});demo

laravel5.1 自写类加载lib

1. app 下新建 libs 目录

app/libs

2. 往该目录添加类文件

3. 修改 composer.json 文件, 在

autoload -> classmap下新增, 目录配置 “app/libs”, 结果大致如下:

“autoload”:{“classmap”:[“app/commands”,”app/controllers”,”app/libs”,”app/models”,”app/database/migrations”,”app/database/seeds”,”app/tests/TestCase.php”]},

4. 执行 composer 更新 autoload:

composer dumpautoload

5、引用

直接在控制器use App\Libraries\TestCase;

之后再实例化即可

laravel5.1 acl权限管理

1.创建角色与权限表

php artisan make:migration create_permissions_and_roles

public function up()

{

Schema::create(‘roles’, function (Blueprint $table) {

$table->increments(‘id’);

$table->string(‘name’);

$table->string(‘label’);

$table->string(‘description’)->nullable();

$table->timestamps();

});

Schema::create(‘permissions’, function (Blueprint $table) {

$table->increments(‘id’);

$table->string(‘name’);

$table->string(‘label’);

$table->string(‘description’)->nullable();

$table->timestamps();

});

Schema::create(‘permission_role’, function (Blueprint $table) {

$table->integer(‘permission_id’)->unsigned();

$table->integer(‘role_id’)->unsigned();

$table->foreign(‘permission_id’)

->references(‘id’)

->on(‘permissions’)

->onDelete(‘cascade’);

$table->foreign(‘role_id’)

->references(‘id’)

->on(‘roles’)

->onDelete(‘cascade’);

$table->primary([‘permission_id’, ‘role_id’]);

});

Schema::create(‘role_user’, function (Blueprint $table) {

$table->integer(‘user_id’)->unsigned();

$table->integer(‘role_id’)->unsigned();

$table->foreign(‘role_id’)

->references(‘id’)

->on(‘roles’)

->onDelete(‘cascade’);

$table->foreign(‘user_id’)

->references(‘id’)

->on(‘users’)

->onDelete(‘cascade’);

$table->primary([‘role_id’, ‘user_id’]);

});

}

public function down()

{

Schema::drop(‘roles’);

Schema::drop(‘permissions’);

Schema::drop(‘permission_role’);

Schema::drop(‘role_user’);

}

2. 创建模型

php artisan make:model Permission

php artisan make:model Role

然后分别打开Permission.php、Role.php 以及 User.php ,加入下面的代码:

// Permissions.php

public function roles()

{

return $this->belongsToMany(Role::class);

}

// Role.php

public function permissions()

{

return $this->belongsToMany(Permission::class);

}

//给角色添加权限

public function givePermissionTo($permission)

{

return $this->permissions()->save($permission);

}

// User.php

public function roles()

{

return $this->belongsToMany(Role::class);

}

// 判断用户是否具有某个角色

public function hasRole($role)

{

if (is_string($role)) {

return $this->roles->contains(‘name’, $role);

}

return !! $role->intersect($this->roles)->count();

}

// 判断用户是否具有某权限

public function hasPermission($permission)

{

return $this->hasRole($permission->roles);

}

// 给用户分配角色

public function assignRole($role)

{

return $this->roles()->save(

Role::whereName($role)->firstOrFail()

);

}

之后就给使用Laravel提供的Authorization来定义权限控制了,打开 /app/Providers/AuthServiceProvider.php 文件,在 boot() 中添加代码:

public function boot(GateContract $gate)

{

parent::registerPolicies($gate);

$permissions = \App\Permission::with(‘roles’)->get();

foreach ($permissions as $permission) {

$gate->define($permission->name, function($user) use ($permission) {

return $user->hasPermission($permission);

});

}

}

3. 填充数据

这里使用 tinker 命令行工具来添加几条测试数据:

php artisan tinker

// 改变命名空间位置,避免下面每次都要输入 App

namespace App

// 创建权限

$permission_edit = new Permission

$permission_edit->name = ‘edit-post’

$permission_edit->label = ‘Can edit post’

$permission_edit->save()

$permission_delete = new Permission

$permission_delete->name = ‘delete-post’

$permission_delete->label = ‘Can delete post’

$permission_delete->save()

// 创建角色

$role_editor = new Role

$role_editor->name = ‘editor’;

$role_editor->label = ‘The editor of the site’;

$role_editor->save()

$role_editor->givePermissionTo($permission_edit)

$role_admin = new Role

$role_admin->name = ‘admin’;

$role_admin->label = ‘The admin of the site’;

$role_admin->save()

// 给角色分配权限

$role_admin->givePermissionTo($permission_edit)

$role_admin->givePermissionTo($permission_delete)

// 创建用户

$editor = factory(User::class)->create()

// 给用户分配角色

$editor->assignRole($role_editor->name)

$admin = factory(User::class)->create()

$admin->assignRole($role_admin->name)

上面我们创建了两个权限:edit-post 和 delete-post,然后创建了 editor 和 admin 两个角色,editor 角色拥有 edit-post 的权限,而 admin 两个权限都有。之后生成了两个用户,分别给他们分配了 editor 和 admin 的角色,即:ID 1 用户拥有 editor 角色,因此只有 edit-post 权限,而 ID 2 用户拥有 admin 角色,因此具有 edit-post 和 delete-post 权限。下面我们来验证下是否正确。

打开 routes.php 文件:

Route::get(‘/’, function () {

$user = Auth::loginUsingId(1);

return view(‘welcome’);

})

上面我们先验证 ID 1 用户的权限,然后修改 /resources/views/welcome.blade.php 文件:

<html>

<head>

<title>Laravel</title>

</head>

<body>

<h1>权限测试</h1>

<p>

@can(‘edit-post’)

<a href=”#”>Edit Post</a>

@endcan

</p>

<p>

@can(‘delete-post’)

<a href=”#”>Delete Post</a>

@endcan

</p>

</body>

</html>

打开浏览器,访问上面定义的路由,可以看到视图中只出现了 Edit Post 链接。之后我们修改路由中用户ID为 2 ,然后再次刷新浏览器,可以看到,这次同时出现了 Edit Post 和 Delete Post 两个链接,说明我们定义的权限控制起作用了。

 

%e9%ba%a6%e5%ba%93%e6%88%aa%e5%9b%be20161501155400388

http://www.iteye.com/magazines/82 原理

linux centos7 lnmp nginx + laravel5.1设置重定向

server {
listen 80;
server_name example.com;
location / {
root /data/lnmp/nginx/burj/public;
try_files $uri $uri/ /index.php?$query_string;
index index.php index.html index.htm;
}
error_page 404 /404.html;
location = /404.html {
root /data/lnmp/nginx/burj/public;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/lnmp/nginx/html/laravel/public;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /data/lnmp/nginx/burj/public;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

https://laravel-china.org/topics/1247 这是别人写好的,挺完整的转载过来,感谢他的分享
如果还出错,那就是权限的问题