hakopako

Full-stack Engineer's blog

Auth with Sentinel + SQLite3 + PHP7

日本語 | English

Sentinel is an authorization and authentication package.
I tried it out on a prototype product, but constructing MySQL or PostgreSQL is too much...
Then I found out to use sqlite3.

Install Sentinel

As the official page says, install Sentinel and Illuminate with composer. I won't write the detail of how to install packages with composer, but composer.json would be like below.

# composer.json
~~~ ... ~~~
    "require": {
        "cartalyst/sentinel": "2.0.*", 
        "illuminate/database": "~5.1",
        "illuminate/events": "^5.2",
        "illuminate/http": "~5.0"
    },
~~~ ... ~~~
$ php composer.phar install

Configure DB connection of Sentinel

For the details, visit official page: https://cartalyst.com/manual/sentinel/2.0#configuration

// Import the necessary classes
use Cartalyst\Sentinel\Native\Facades\Sentinel;
use Illuminate\Database\Capsule\Manager as Capsule;

// Include the composer autoload file
require 'vendor/autoload.php';

// Setup a new Eloquent Capsule instance
$capsule = new Capsule;

$capsule->addConnection([
    'driver'    => 'sqlite',
    'database'  => '/path/to/database.sqlite',
    'charset'   => 'utf8'
]);

$capsule->bootEloquent();

These 2 lines are changed.

  • 'driver' => 'sqlite',
  • 'database' => '/path/to/database.sqlite',

Create User Tables

Sentinel supports migrate function of Laravel Framework.
As long as you work with Sentinel on Laravel and the database is such as MySQL, user tables are created automatically with prepared migration command.
For here, I'm going to use sqlite3 instead of MySQL, so creating user tables will be manual.

I wrote a SQL Schema file which is translated from MySQL Schema*1 into SQLite Schema.
Execute this SQL on your sqlite3 database to create user tables which Sentinel access to.

This is the SQLite Schema for Sentinel.

Check the behavior

Write and run functions as the way official page says, you'll see it works.
This is an example usage script.

// Register a new user
Sentinel::register([
    'email'    => 'test@example.com',
    'password' => 'foobar',
]);


// Authenticate
$credentials = [
    'email'    => 'test@example.com',
    'password' => 'foobar',
];

echo Sentinel::authenticate($credentials);

That's all.

Install php7.1.9 with phpenv on MacOS

日本語 | English

I tried to install php7.1.9 with phpenv on MacOS(Sierra/10.12.3), but some errors occurred. This article is a note of it.

Install phpenv

Brew didin't support to install phpenv itself. So I installed it manually like below.

$ cd
$ git clone https://github.com/CHH/phpenv.git
$ cd phpenv/bin
$ ./phpenv-install.sh
# ---> $HOME/.phpenv  is created.

# clean up 
$ cd 
$ rm -rf phpenv

Set PATH for phpenv command.

$ echo 'export PATH="$HOME/.phpenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(phpenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile
$ phpenv versions
* system (set by /Users/XXXX/.phpenv/version)

phpenv install 7.1.9

Clone php-build repository in order to build php later.

$ git clone https://github.com/CHH/php-build.git ~/.phpenv/plugins/php-build

Check installable php version list.

$ phpenv install --list
Available versions:
  5.2.17
  5.3.10
  5.3.11
~~~ .... ~~~
  7.1.9

This time, I'm installing version 7.1.9.

$ phpenv install 7.1.9

~~~ .... ~~~

-----------------
|  BUILD ERROR  |
-----------------

Here are the last 10 lines from the log:

-----------------------------------------
configure: WARNING: This bison version is not supported for regeneration of the Zend/PHP parsers (found: 2.3, min: 204, excluded: ).
configure: WARNING: You will need re2c 0.13.4 or later if you want to regenerate PHP parsers.
configure: error: OpenSSL version 1.0.1 or greater required.
-----------------------------------------

As the error above says, some packages are missing.
Install them with brew comannd like below.

$ brew install gcc re2c libmcrypt autoconf automake libiconv bison openssl libxml2 jpeg libpng
$ brew link --force bison
$ brew link --force openssl
$ brew link --force libxml2

Try install php 7.1.9 again.

$ phpenv install 7.1.9
[Info]: Loaded extension plugin
[Info]: Loaded apc Plugin.
[Info]: Loaded composer Plugin.
[Info]: Loaded github Plugin.
[Info]: Loaded uprofiler Plugin.
[Info]: Loaded xdebug Plugin.
[Info]: Loaded xhprof Plugin.
[Info]: Loaded zendopcache Plugin.
[Info]: php.ini-production gets used as php.ini
[Info]: Building 7.1.9 into /Users/XXXX/.phpenv/versions/7.1.9
[Skipping]: Already downloaded and extracted https://secure.php.net/distributions/php-7.1.9.tar.bz2
[Preparing]: /var/tmp/php-build/source/7.1.9
[Compiling]: /var/tmp/php-build/source/7.1.9
[Success]: Built 7.1.9 successfully.

It seems to be succeeded.
Let's check php 7.1.9 behavior.

$ phpenv versions
* system (set by /Users/XXXX/.phpenv/version)
  7.1.9
$ phpenv global 7.1.9
$ php -v
PHP 7.1.9 (cli) (built: Sep 16 2017 15:51:12) ( NTS )

That' all.

How to use pyenv with cygwin - windows

日本語 | English

On Linux or MacOS, we can install and set up pyenv with prepared commands such as "brew". However, Windows doesn't go that way. The reason is that python-build doesn't work on windows. We can use pyenv with cygwin by doing that process ($ pyenv install X.X.X ) manually.

install pyenv

Clone github repository, and set PATH.

# Cygwin
$ cd  
$ git clone https://github.com/pyenv/pyenv.git .pyenv     
$ echo 'export PYENV_ROOT="$HOME/.pyenv"' >> .profile  
$ echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> .profile  
$ echo -e 'eval "$(pyenv init -)"' >> .profile  
$ source .profile
$ pyenv versions
* system (set by /home/XXXXXXXXX/.python-version)

This error might occur.

/path/to/.pyenv/bin/pyenv: line 1: ../libexec/pyenv: No such file or directory

This is because of "Failed to create symbolic link". So just do it manually like below.

$ cd
$ touch /bin/readlink  
$ rm -rf ~/.pyenv/bin/pyenv   
$ ln -s ~/.pyenv/libexec/pyenv ~/.pyenv/bin/pyenv  

Check if pyenv works.

$ pyenv versions
* system (set by /home/XXXXXXXXX/.python-version)

pyenv install X.X.X

As I mentioned earlier, python-build doesn't work on windows. Do that process manually (I couldn't find any other good way...).

Version 2.X

  • Download Python Installer from official page.
    Windows x86-64 MSI installer: https://www.python.org/downloads/release/python-2712/
  • Launch the installer and set the install path to pyenv directory.
    PATH: /path/to/.pyenv/versions/2.7.12
  • Probably creating a symbolic link is failed. Do it manually like below.
$ cd .pyenv/versions/2.7.12
$ mkdir bin && cd bin
$ ln -s ../python.exe python

Check the behavior.

$ cd
$ pyenv local 2.7.12
$ pyenv versions
  system
* 2.7.12 (set by /home/XXXXXXXXX/.python-version)
$ python -V
Python 2.7.12

Version 3.X

Version 3.X is also able to be installed with the installer from official page as same as version 2.X. However, as for version 3.X, here is another solution.

"Embeddable file" is distributed officially. Therefore, what we do is decompress the file and put it to pyenv directory.

  • Download embeddable zip file
    Windows x86-64 embeddable zip file: https://www.python.org/downloads/release/python-362/
  • Decompress the file and put it to pyenv directory.
    PATH: /path/to/.pyenv/versions/3.6.2
  • Probably creating a symbolic link is failed. Do it manually like below.
$ cd .pyenv/versions
$ mv /path/to/python-3.6.2-embed-amd64 3.6.2
$ mkdir 3.6.2/bin && cd 3.6.2/bin
$ ln -s ../python.exe python

Check the behavior.

$ cd
$ pyenv local 3.6.2
$ pyenv versions
  system
  2.7.12
* 3.6.2 (set by /home/XXXXXXXXX/.python-version)
$ python -V
Python 3.6.2

 

That is all.