手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表分类:PHP

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了一下,没排版,要看详细的,还是直接看原版吧:

XML/HTML代码
  1. Valet is great.  
  2. 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).  
  3. 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.  
  4. 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.  
  5. Dnsmasq  
  6. First we’ll need to update the Dnsmasq config so that it will handle multiple TLDs.  
  7. Turns out this is really easy. Valet holds some config in ~/.config/valet/dnsmasq.conf.  
  8. 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.  

  9. address=/{tld}/127.0.0.1  
  10. listen-address=127.0.0.1  

  11. Then restart Dnsmasq:  

  12. sudo brew services restart dnsmasq  

  13. Valet  
  14. Now we just need to update the Valet code to support multiple domains.  
  15. 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.  

  16. {  
  17.     ...  
  18.     "tlds": [  
  19.          "tld1",  
  20.          "tld2"  
  21.     ]  
  22. }  

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

  25. // server.php, line 70  
  26. $siteName = basename(  
  27.     // Filter host to support wildcard dns feature  
  28.     valet_support_wildcard_dns($_SERVER['HTTP_HOST']),  
  29.     '.'.$tld  
  30. );  
  31. // change to:  
  32. foreach ($valetConfig['tlds'] as $tld) {  
  33.     if (strpos($_SERVER['HTTP_HOST'], '.'.$tld) === false) {  
  34.         continue;  
  35.     }  
  36.     $siteName = basename(  
  37.         // Filter host to support wildcard dns feature  
  38.         valet_support_wildcard_dns($_SERVER['HTTP_HOST']),  
  39.         '.'.$tld  
  40.     );  
  41. }  

  42. This just allows the server to respond to whichever one of the TLDs we want to support.  
  43. 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:  

  44. // cli/valet.php, line 145, secure command  
  45. $app->command('secure [domain]', function ($domain = null) {  
  46.     $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];  
  47.     Site::secure($url);  
  48.     Nginx::restart();  
  49.     info('The ['.$url.'] site has been secured with a fresh TLS certificate.');  
  50. })->descriptions('Secure the given domain with a trusted TLS certificate');  
  51. // change to:  
  52. $app->command('secure [domain]', function ($domain = null) {  
  53.     $urls = [];  
  54.     foreach (Configuration::read()['tlds'] as $tld) {  
  55.         $url = ($domain ?: Site::host(getcwd())).'.'.$tld;  
  56.         Site::secure($url);  
  57.         $urls[] = $url;  
  58.     }  
  59.     Nginx::restart();  
  60.     $urls = implode(', ', $urls);  
  61.     info('The ['.$urls.'] site has been secured with a fresh TLS certificate.');  
  62. })->descriptions('Secure the given domain with a trusted TLS certificate');  

  63. And now the unsecure command:  
  64. // cli/valet.php, line 158, unsecure command  
  65. $app->command('unsecure [domain]', function ($domain = null) {  
  66.     $url = ($domain ?: Site::host(getcwd())).'.'.Configuration::read()['tld'];  
  67. Site::unsecure($url);  
  68. Nginx::restart();  
  69. info('The ['.$url.'] site  will now serve traffic over HTTP.');  
  70. })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');  
  71. // change to:  
  72. $app->command('secure [domain]', function ($domain = null) {  
  73.     $urls = [];  
  74.     foreach (Configuration::read()['tlds'] as $tld) {  
  75.         $url = ($domain ?: Site::host(getcwd())).'.'.$tld;  
  76.         Site::secure($url);  
  77.         $urls[] = $url;  
  78.     }  
  79.     Nginx::restart();  
  80.     $urls = implode(', ', $urls);  
  81.     info('The ['.$urls.'] site will now serve traffic over HTTP.');  
  82. })->descriptions('Stop serving the given domain over HTTPS and remove the trusted TLS certificate');  

  83. Finally, run the following command:  

  84. valet restart 

  85. Enjoy

 

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

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

 

Tags: valet, docker, frp

用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僵尸进程并干掉它

 几个常用命令

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

Tags: strace, pstree

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 默认是通过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

不多说,试试看吧

Records:64712345678910»