PHP

浏览模式: 标准 | 列表 | 图文
COPY:Supporting multiple TLDs in Laravel Valet

COPY:Supporting multiple TLDs in Laravel Valet

因为最近在用valet,然后把MAMP PRO都干掉了(不能怪我啊,实在是MAMP PRO6的一些使用太反人类了。。。不知道作者是怎么想的),但现在想想反正也就是这样简单的使用,干脆就用valet了算了。其实也可以用laravel sail的。但因为4月份之前,M1版的mac上的docker都不正常。所以,就暂时放弃了,现在用用valet也算OK。80%的情况下都OK

创建个目录,valet link一下就完事了。因为现在都是laravel的项目,用起来也算方便,毕竟也是官方的工具。但因为我用frp,所以我设置valet tld 真实的域名(比如:xxx.com),这就造成,我访问demo.xxx.com,访问的是127.0.0.1,然后我映射在外面的server也是demo.xxx.com,其实这一切都OK了。然而问题是,有时候我要测试真实域名,比如就是 demo.xxx.com ,我就只能打开全局梯子。这样我访问demo.xxx.com才对应了 frp的域名。

听起来有点绕口令,说白了就是,因为用了真实域名,所以我默认访问都是本地,但外网访问我都是真实的。而我如果要测试真实的网址,反而还要爬个梯子。虽然影响不大,但还是想换换,于是我就想,valet 是不是可以允许绑定两个域名?这样的话,是不是就全解决了?本地测试一个域名,一个域名公开对外。

找了一些文章,发现默认是不支持,但有人通过hack的方法(说白了就是改模板,改配置改代码。。。。)支持了。原文在这里:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613

我简单的COPY了一下,没排版,要看详细的,还是直接看原版吧:

Valet is great.
Occasionally though I need an app to respond on a different TLD to the default one. And there are even times when it makes sense to have an app respond on multiple TLDs (e.g. to prove some multi-domain functionality).
You may have some secure assets hosted by a third-party that requires domain verification (FontAwesome Pro). In development, that may mean it’s easier to be using the same domain as other devs on the team than add loads of different dev domains to the service’s whitelist.
In the current release of Valet, this kind of functionality isn’t natively available. Even in third-party packages like Valet+, this isn’t supported. But with a few quick changes it’s easily possible. Let me show you how.
Dnsmasq
First we’ll need to update the Dnsmasq config so that it will handle multiple TLDs.
Turns out this is really easy. Valet holds some config in ~/.config/valet/dnsmasq.conf.
In there we just need to add some config for each TLD we want to support. Add the following two lines for every TLD you want to support. Replace {tld} with the TLD, unsurprisingly.

address=/{tld}/127.0.0.1
listen-address=127.0.0.1

Then restart Dnsmasq:

sudo brew services restart dnsmasq

Valet
Now we just need to update the Valet code to support multiple domains.
Firstly, we’re going to add some values to our ~/.config/valet/config.json. I’ve opted for simply adding keys rather than changing any existing one. I’ve added a tlds array. This will be used later.

{
...
"tlds": [
"tld1",
"tld2"
]
}

Then we just need to edit some Valet files and run some commands.
All of these edits I’m making directly in my globally-installed Valet (~/.composer/vendor/laravel/valet/).

// server.php, line 70
$siteName = basename(
// Filter host to support wildcard dns feature
valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
'.'.$tld
);
// change to:
foreach ($valetConfig['tlds'] as $tld) {
if (strpos($_SERVER['HTTP_HOST'], '.'.$tld) === false) {
continue;
}
$siteName = basename(
// Filter host to support wildcard dns feature
valet_support_wildcard_dns($_SERVER['HTTP_HOST']),
'.'.$tld
);
}

This just allows the server to respond to whichever one of the TLDs we want to support.
Now let’s update the CLI so we can generate and remove SSL certs for all of the TLDs (valet secure and valet unsecure commands). Firstly, secure:

// cli/valet.php, line 145, secure command
$app->command('secure [domain]', function ($domain = null) {
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
Site::secure($url);
Nginx::restart();
info('The ['.$url.'] site has been secured with a fresh TLS certificate.');
})->descriptions('Secure the given domain with a trusted TLS certificate');
// change to:
$app->command('secure [domain]', function ($domain = null) {
$urls = [];
foreach (Configuration::read()['tlds'] as $tld) {
$url = ($domain ?: Site::host(getcwd())).'.'.$tld;
Site::secure($url);
$urls[] = $url;
}
Nginx::restart();
$urls = implode(', ', $urls);
info('The ['.$urls.'] site has been secured with a fresh TLS certificate.');
})->descriptions('Secure the given domain with a trusted TLS certificate');

And now the unsecure command:
// cli/valet.php, line 158, unsecure command
$app->command('unsecure [domain]', function ($domain = null) {
$url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];
Site::unsecure($url);
Nginx::restart();
info('The ['.$url.'] site will now serve traffic over HTTP.');
})->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');
// change to:
$app->command('secure [domain]', function ($domain = null) {
$urls = [];
foreach (Configuration::read()['tlds'] as $tld) {
$url = ($domain ?: Site::host(getcwd())).'.'.$tld;
Site::secure($url);
$urls[] = $url;
}
Nginx::restart();
$urls = implode(', ', $urls);
info('The ['.$urls.'] site will now serve traffic over HTTP.');
})->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');

Finally, run the following command:

valet restart

Enjoy

以上是仅复制未排版,嫌丑的看原文:https://medium.com/@simonhamp/supporting-multiple-tlds-in-laravel-valet-e3d2a963c613

如果不用梯子看不了,就将就点吧

用ziggy来管理前端代码中的路由。

用ziggy来管理前端代码中的路由。

突然间不知道这个算是该放在哪个分类了,毕竟ziggy在github上的项目,也是src为PHP,dist是JS

ziggy是一个优秀的、配合laravel使用的、适合用于SAP项目的一个前端组件。对于laravel来说,他只是主要实现了一个@route的方法,放在blade模板里就行了。至于是全放还是部分放,那就看你怎么配置的了:https://github.com/tighten/ziggy#filtering-routes,比如debug相关的就应该去掉。如果前后端项目有分离,那更应该用@route(['group'])的方式来处理。
对于前端来说,那就更方便了,他提供了一个route的方法,可以直接用laravel路由的名字。比如route('home.index'),会生成相应的路由。如果你要用在axios或者其他里面,你得 使用url()方法,比如route('home.index').url()生成一个字符串。
ziggy的用法有很多,即使你不使用blade模板,官方也都提供了适用的方法,说多了也没啥用,看这里就行了:https://github.com/tighten/ziggy#filtering-routes
常用方法:
route('home.index') => 对应 Route::get()->name('home.index')
route('home.index',[1,2]) =>对应 Route::get('/aaa/{a}/bbb/{b}')->name('home.index') ,允许传入数据当成 参数传递
route('home.index' , {a:1,b:2}) ,同样对应上面的路由,也就是说他第二个参数除了可以传入数组外,也可以传入对象
如果你有用laravel 的route.binding功能,他生成链接的时候,会根据你在Model里的getRouteKey来输出URL,参考:https://github.com/tighten/ziggy#route-model-binding
更多的还是看官方吧。。。。
查看PHP-fpm僵尸进程并干掉它

查看PHP-fpm僵尸进程并干掉它

几个常用命令

1、pstree -ap|grep php ,这是以树型进行展示 ,如果有括号,基本上就是僵尸
2、ps -A -o stat,ppid,pid,cmd | grep -e '^[zZ]' ,如果有,那就是僵尸。毕竟都Z了
有兴趣的在遇到僵尸后可以strace -p跟进一下,没兴趣就直接:kill -HUP id 或者 kill -9 id了
当然,最好还是strace一下,这样方便排查问题,而不至于后面会频繁出现。遇到问题不要急着重启nginx和php-fpm,排查一下,不然下次可能还会出现
其他方法
1、ps -aux|grep -v "grep\|nginx\|php-fpm" | grep php
Laravel 修改 Hash的默认driver 为Md5

Laravel 修改 Hash的默认driver 为Md5

写这一篇记录的时候,是因为在尝试用Nova做后台,因为是基于老的数据库,所以用户这一块在登录的时候判断密码是否正确就验证不通过了。默认laravel的hashing用的是bcrypt,而老数据库,大家都懂的,明显是md5,然后是不是加盐(salt),就看实际情况了。

对于Laravel来说,hasher的调整是在app/config/hashing.php里进行配置,但默认的是bcrypt。配置文件里直接就说了,【Supported: "bcrypt", "argon", "argon2id"】,那,MD5怎么办?

如果只是为了登录,那当然直接改Login方法就行了。如果不改Login方法,甚至想一行代码都不改,最好就是实现一个md5的驱动,同时让driver设置为md5即可。

网上的教程都是写一个Md5ServiceProvider,然后app->make('hash'),将它直接修改为Md5。这样带来的另一个问题,你反而不能直接用一些默认的方法了,比如Hash::make和Hash::check,因为整个hash都替换了。

正确的方法是,实现一个Md5的Hasher,然后,扩展一个方法出来,相当于实现了一个md5的driver,这时候再调整hashing中将driver设置为md5即可。此时,你再调用Hash::make,返回的就是md5后的结果,也支持参数(参数功能需要自己开发,比如 Hash::make('123',['salt'=>11]),即为md5加盐,也可以支持type ='suffix' 或者其他,甚至可以 double salt。so easy ~~~~)

---EOF

没有附上代码,因为实现MD5功能实在太简单了,关键的就是不要破坏性的增加或者替换功能,这才是重中之重。

laravel 插件推荐:kitloong/laravel-migrations-generator

laravel 插件推荐:kitloong/laravel-migrations-generator

Laravel 默认是通过migration来创建表,大部分情况下这并没有问题,然而如果是针对一个已经存在的项目想要做迁移,那就需要将DB转换成migration文件。早些年没有什么特别好的,写debugbar的那位写了一个,但已常年不更新了。后面sequel pro有一个插件,可以将指定的表转成migration文件,但这会有一个小小的问题,即:得安装sequel pro,还得更新插件。那究竟 没有一个好的laravel插件来做这个呢?

其实现在已经有了,打开:https://github.com/kitloong/laravel-migrations-generator,你就会看到了。在最初的时候,我是搜索到这个:https://github.com/barryvdh/laravel-migration-generator,上面有提示切换成:https://github.com/Xethron/migrations-generator,然而,这个大神现在也不更新了。在laravel 5之后就没有再更新过。现在这个kitloong的就是基于Xethron的库而进行更新的。

用法也很简单,composer require --dev "kitloong/laravel-migrations-generator",然后直接命令行运行:php artisan help migrate:generate,可以看帮助。如果没有特别的需求,直接php artisan migrate:generate完事。如果只想更新指定的表,那就:php artisan migrate:generate -t tablename

不多说,试试看吧

PHP查询的时候GroupBy错误

PHP查询的时候GroupBy错误

数据库查询的时候,报:group by 错误,也就是ONLY_FULL_GROUP_BY。

要么去改my.cnf,要么pdo的ATTR中也可以增加,但线上用的是一个很老的TP版本,而且受影响的字段不多,直接就在不需要group by的字段上加个方法:any_value进行解决。
例如:Db::table('model')->field('any_value(id) as id,any_value(title) as title, name')->group('name')->select();
Git:Out of memory, malloc failed解决

Git:Out of memory, malloc failed解决

本来我是应该不会遇到这种问题的吧?只是在用composer的时候,因为dcat-admin经常更新,但release版本又比较慢。所以就直接引用他的git了。本地无所谓,毕竟我有32G内存,上线后,在composer update的时候,报out of memory.

网上不少的例子都是说:git config --global pack.windowMemory 1024m,我这样试了之后没有解决
于是:

git config --global pack.windowMemory 1g
git config --global pack.packSizeLimit 1g

git config --global pack.deltaCacheSize 1g

git config --global core.packedGitLimit 1g

git config --global core.packedGitWindowSize 1g

还是没有用。再找资料的时候有看到说,可能是因为没有swap空间,即使你的内存大,没有swap也可能会有问题。于是看了一下,确实:free -m的时候。swap确实是0,参考:https://stackoverflow.com/questions/14038074/git-pull-fatal-out-of-memory-malloc-failed

好吧,几步操作:

1、dd if=/dev/zero of=/root/myswapfile bs=1M count=1024

2、chmod 600 /root/myswapfile

3、mkswap /root/myswapfile

4、swapon /root/myswapfile

5、vim /etc/fstab
/root/myswapfile swap swap defaults 0 0

然后就一切OK了,没有好好排版。因为,断断续续的写了三天。中间还断过一次。忍忍吧(等什么时候换laravel后台再好好弄弄)

Dcat-Admin 的 multipleSelect和select 在编辑的时候如何赋值?

Dcat-Admin 的 multipleSelect和select 在编辑的时候如何赋值?

Dcat-Admin的multipleSelect和select 在新增的时候,可以通过default()方法给它赋值,但在编辑的时候,不能通过default方法赋值。

1、如果编辑的时候,默认数据库取出来的值,对应着 options中的key,可以直接使用。

2、如果需要调整,比如存到数据库时,是字符串型,但select/multipleSelect的value只能是数组。如果通过->value([$data])赋值也是无效的。只能通过customFormat方法进行赋值。默认的参数就是传进来的值。如果你是字符串,可以explde,如果是对象,可以遍历后返回。

参考 :http://www.dcatadmin.com/docs/master/model-form-fields.html

Dcat-Admin with vue

Dcat-Admin with vue

有时候你不得不承认,DCAT-admin / laravel-admin 做后台是属于比较方便的。你硬要说nova之类的,我也无话可说。但毕竟nova要钱,而且文档并没有国内的全。

为什么说dcat-admin,主要还是laravel-admin好久没有大更新了。然后laravel-admin的多应用要钱。虽然我买了,但更新太慢,我现在还是用dcat了。

很多人在github上面问,为什么不支持VUE。其实也能理解 ,vue如果用了, pjax就不太好用了。但我还是得说,如果你只是简单的,不做特别复杂的应用,还是有办法支持vue的。

首先全局引用JS,Admin::js(),这个不用多说,如果不想全局引用,那就在自己的页面里,将它引用出来就行了。

然后用了个简单的demo,

<div id="sign-form">
@{{ form }}
<el-input v-model="input" placeholder="请输入内容"></el-input>

<el-steps :active="active" finish-status="success">
<el-step title="步骤 1"></el-step>
<el-step title="步骤 2"></el-step>
<el-step title="步骤 3"></el-step>
</el-steps>

<el-button style="margin-top: 12px;" @click="next">下一步</el-button>

</div>
<script>
var form = {};
new Vue({
el: '#sign-form',
data() {
return {
form: form,
input: '{{date('Y-m-d H:i:s')}}',
active: 0
};
},
mounted() {
this.request();
},
methods: {
formReset() {
this.form = form;
},
request() {
this.form = {a: 1, b: 2, c: 3, d: 5};
{{--laravel.get('{{api_url('union/bind/check/123')}}', res => {--}}
{{-- console.log(res);--}}
{{--});--}}
},
next() {
if (this.active++ > 2) this.active = 0;
}
}
});
</script>

保存后,点开网页看看。确实不会再加载了。但这种VUE,就只适合单页面用用了(当然,本身也就是为了在复杂的表单上使用它。也够了)

---差点没有保存。。

composer clear-cache

composer clear-cache

看起来这个标题好象没啥,今天被折腾了好久

因为网络不好,所以在更新的时候一直更新不下来。再更新就报zip包出错,多次尝试都是这样。
删除vendor目录,再试也是这样
开始以为是墙的问题,毕竟压缩包都在github上,所以爬了梯子试了下,还是zip包出错
因为发现问题都是出现在load from cache,所以想了下,还是用composer clear-cache清空一下。然后composer global update,再回到本目录,进行composer update
一切OVER