1. Keep learning forward

    To be updated ...
  2. #TIL : View real-time logs using websocketd

    I found a handy tool for making a websocket right on your shell.
    It is websocketd : http://websocketd.com/

    Its phisolophy is so simple :

    Just read incoming text from stdin and write outgoing text to stdout. Messaging is simple.

    Read its docs, I follow the ez tutorial 10 second tutorial and see how cool does it work.
    Let make something cool with this, we got a UNIX tool that print out to stdout in real-time with changes of end file.

    Ok cool, change the command to

    websocketd --port=8080 tail -f logs/web_access.log

    Tada ! Open the webpage that listens the websocket you made and see the magic :D You can tracking access log of website or system in real-time way.

  3. #TIL : Run built-in server via Docker

    Docker is the fast and clean way to run Linux programs.

    We can run a PHP project via PHP built-in server and Docker.

    docker run -it -p 8080:8080 -v `pwd`:/code php:7 php -S 0.0.0.0:8080 -t /code/web /code/web/server.php

    With server.php content is

    <?php
    
    $filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
    if (php_sapi_name() === 'cli-server' && is_file($filename)) {
        return false;
    
    }
    
    // Run application below
    $app = new Application();
    $app->run();
    
  4. #TIL : Reference assign object variable

    When you have a object x and assign y = x, y will be a ref of x (it looks like pointer of C). So changing property of y means changing property of x.

    Ex :

    x = {"a": 1, "b": 2}
    y = x
    y['a'] = 100
    print x['a'] # Result is 100

    So if you want clone the value, use copy lib :

    import copy
    x = {"a": 1, "b": 2}
    y = copy.deepcopy(x)
    y['a'] = 100
    print x['a'] # Result is 1
  5. #TIL : FTP via curl tool

    Can upload an file via FTP by curl tool with handy script :

    curl -T file_need_to_upload ftp://hostname --user user:passwd
  6. #TIL : Eloquent Many-to-Many Relationship

    To create the n-to-m relationship in Eloquent, we create a table stand between 2 tables.
    Eg:

    We have the db schema :

    postpost_tagtag
    id (PK)id (PK)id (PK)
    titlepost_id (Index)name (Unique)
    contenttag_id (Index)

    In the model Post and Tag, we define a relation :

    • App\Model\Post.php
    public function tags() {
        return $this->belongsToMany('App\Model\Tag', 'post_tag', 'post_id', 'tag_id');
    }
    • App\Model\Tag.php
    public function posts() {
        return $this->belongsToMany('App\Model\Post', 'post_tag', 'tag_id', 'post_id');
    }

    After that, we can use the relation to fetch the related data, like

    $post = Post::firstOrFail(1);
    $tags = $post->tags; // return the Collections class contains tags

    or

    $tag = Tag::where('name', '=', 'php')->first();
    $posts_of_tag = $tag->posts; // return the Collections class contains posts

    When updating or creating post, we have to sync with tags by

    $tag_ids = [2, 3 ,4]; // Get tag ids from tag names
    $post->tags()->sync($tag_ids);
  7. #TIL : String Format Unicode params

    unicode_thing = u"Xin chào mọi người"
    a = '{}'.format(unicode_thing)

    will cause the error UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 6: ordinal not in range(128)

    The solution is add u prefix the pattern (it means using unicode pattern) :

    unicode_thing = u"Xin chào mọi người"
    a = u'{}'.format(unicode_thing)
  8. #TIL : Index is useless when use function on indexed field

    Reality, if using function on indexed field, you will broke indexing by accident.
    Eg:

    WHERE MONTH(`date`) = 11 AND YEAR(`date`) = 2015

    Solution is transform the query to comparison query, like this :

    WHERE `date` >= '2015-11-01' AND `date` < '2015-12-01'
  9. #TIL : Shortcut keyboard improve productivity

    • Open the preference of any Application by Cmd + ,.
    • Press Cmd + ~ to go previous App when switching App on Cmd + Tab
    • Copy screenshot to clipboard by Cmd + Ctl + Shift + 3
    • Press Option when click to notification center is putting it on “Do no disturb” mode.
  10. #TIL : Debugging Chrome extension

    • To open directly the Console Dev Tools, press Cmd + Opt + J in MacOSX.
    • To debug easily the any files of extension, open the url chrome-extension://<extension-id>/<file-name> in address bar.