• About

Viet PHP

PHP = ['newbie' => 'cơ bản', 'coder' => 'nâng cao', 'professional'=>'chuyên sâu']

  • PHP
    • OOP
    • Basic
  • WordPress
  • Bài tập PHP
  • PHP 7

How to pass a variable to the callback function provided by Laravel Collection

Posted on July 4, 2018 Written by admin Leave a Comment

Laravel Collection consists of a bundle of useful functions that in my opinion is compliment with pure PHP’s array functions in many way. Collection as its name may invoke, is a kind of array in the form of object and rather than accessing to the its value by $array[‘key’] as it is in the pure PHP language, we have to use the object style as $collection->key. I feel quite comfortable to work with both. You can check it out here.

So below is a tiny note I want to keep it for later reference when today I worked with some of the call-back functions provided by Laravel Collection, map() and transform() in particular. These functions allow us to “travel” to each item in the collection so then you can change the value in the collection. However, it seems to me, at first, that we cannot pass a variable into these call-back functions. Here is a basic example about what I have written above:

$messages->transform(function ($item, $key) {

$item->encrypted = sodium_crypto_box_seal_open(decrypt($item->encrypted), decrypt($privatekeyinput));
                                            
                                       return $item;
                                              
   });

What I want to do is to decrypt the encrypted items in the collection named messages. However, I need to use a variable $privatekeyinput that exists in temporary only and it appears that it does not work at all since the call-back function does not find the $privatekeyinput.

I tried to prepend/put another item into the collection it somehow I think I need more time to figure out that way. I also tried to convert the collection into array using toArray() method and I intended to use call-back functions provided by PHP such as array_map() or array_walk() to deal with the array rather than Collection functions given by Laravel. However, as I want to dig a bit deeper into Laravel Collection, I did a search on Google and it turned out that the case could be solved by using a Closure as it is in pure PHP. Then the above code was turned into the following one wherein you can see that a Closure was attached to the call-back function using use($variable).

  $messages->transform(function ($item, $key)use($privatekeyinput) {

                                                $item->encrypted = sodium_crypto_box_seal_open(decrypt($item->encrypted), decrypt($privatekeyinput));
                                            
                                                return $item;
                                              
                                            });

So now the variable $privatekeyinput has been already defined in the call-back function and it works 🙂

Filed Under: Laravel

Cài Sodium trên PHP 7.2 Centos

Posted on June 26, 2018 Written by admin Leave a Comment

Mình đang cần dùng Sodium cho dự án cá nhân đang làm, mặc dù có sẵn trên PHP 7.2, nhưng bình thường vẫn phải cài thêm với tư cách extension. Giả sử mình chạy phpinfo() thì nếu như kết quả như hình dưới thì extension coi như đã cài thành công:

 

extension này nằm trong /etc/php.d, đây là Centos 7.

Trên terminal thì mình có thể kiểm tra bằng : php -i | grep “sodium”.

Nếu kết quả như sau, hoặc tương tự thì okey (code dưới áp dụng với PECL extension).

/etc/php/7.1/cli/conf.d/ext-libsodium.ini,
libsodium
libsodium support => enabled
libsodium compiled version => 2.0.10

Rồi giờ tới việc cài extension này trên Centos 7, dùng nguồn từ remirepo.

  1. Download the latest remi-release rpm from
    http://rpms.famillecollet.com/enterprise/7/remi/x86_64/
  2. Install remi-release rpm:
    # rpm -Uvh remi-release*rpm
  3. Install php72-php-sodium rpm package:
    # yum --enablerepo=remi install php72-php-sodium

Nguồn: https://centos.pkgs.org/7/remi-x86_64/php72-php-sodium-7.2.7-1.el7.remi.x86_64.rpm.html

Theo nguồn tại https://lukasmestan.com/install-libsodium-extension-in-php7/ thì có câu này đáng chú ý: “Version 7.2.0 and newer of the PHP programming language includes the Sodium extension (referred to as ext/sodium) as a core cryptography library. Version 2 of the PHP extension in PECL is compatible with ext/sodium in PHP 7.2.”. Ngoài ra, coi ảnh dưới để thấy sự khác biệt của extension này ở phiên bản 7.2 trở lên. Nguồn tại https://paragonie.com/book/pecl-libsodium/read/00-intro.md

 

Filed Under: Uncategorized

Dependency Injection và Container

Posted on May 29, 2018 Written by admin Leave a Comment

Giả sử ta xây dựng Dependecy Injection với Interface. Hệ thống như sau: - Xây dựng một interface chứa function xử lý. - Class chức năng A, B implements interface này. - Class C trực tiếp sử dụng và sẽ kế thừa tính năng của class A, B. Tuy … [Continue reading]

Filed Under: OOP

Hàm array_map() và array_walk()

Posted on May 23, 2018 Written by admin Leave a Comment

Hai thằng array_walk và array_map khá là giống nhau. 1) giống: cả hai đều nhận parameter là array và một hàm callback | hàm xử lý thứ ba. 2) Khác: + array_map("hàm", $array) có thứ tự parameter ngược với array_walk($array, "hàm"). + … [Continue reading]

Filed Under: Basic, PHP

  • 1
  • 2
  • 3
  • …
  • 8
  • Next Page »

Recent Posts

  • How to pass a variable to the callback function provided by Laravel Collection
  • Cài Sodium trên PHP 7.2 Centos
  • Dependency Injection và Container
  • Hàm array_map() và array_walk()
  • Dependency Injection

Recent Comments

  • admin on Dependency Injection
  • Vượng on Dependency Injection
  • vượng on Dependency Injection
  • vượng on Dependency Injection
  • vượng on Dependency Injection

Archives

  • July 2018
  • June 2018
  • May 2018
  • April 2018

Categories

  • Bài tập PHP
  • Basic
  • Laravel
  • OOP
  • OOP
  • PHP
  • String
  • Uncategorized
  • WordPress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2021 · Focus Pro on Genesis Framework · WordPress · Log in