Saturday, November 21, 2015

Adding localisation to the laravel 5

First, create a new controller "LanguageController". You can use this command : php artisan make:controller LanguageController

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Http\Requests;
use Config;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;

class LanguageController extends Controller {

    public function switchLang($lang)
     {
        if (array_key_exists($lang, Config::get('languages'))) {
            Session::set('applocale', $lang);
        }
        return Redirect::back();
     }
}

Ok, as you see we use a custom config array to fetch the available languages of the app, so lets create a languages.php in the config/ directory :
return [
    'en' => 'English',
    'fr' => 'Français',
];
Good! Now to use our controller, we are going to set up one new route:
Route::get('lang/{lang}', ['as'=>'lang.switch', 'uses'=>'LanguageController@switchLang']);
You should now be able to use a Language dropdown (I often put it in my navbar). Check this example based on boostrap dropdown :
<li class="dropdown">
    <a href="#" class="dropdown-toggle" data-toggle="dropdown">
        {{ Config::get('languages')[App::getLocale()] }}
    </a>
    <ul class="dropdown-menu">
        @foreach (Config::get('languages') as $lang => $language)
            @if ($lang != App::getLocale())
                <li>
                    {!! link_to_route('lang.switch', $language, $lang) !!}
                </li>
            @endif
        @endforeach
    </ul>
</li>
Last thing to do is to set up a new Middleware:

// app/Http/Middleware/Language.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Routing\Middleware;
use Illuminate\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Session;

class Language implements Middleware {
    public function handle($request, Closure $next)
    {
        if (Session::has('applocale') AND array_key_exists(Session::get('applocale'), Config::get('languages'))) {
            App::setLocale(Session::get('applocale'));
        }
        else { // This is optional as Laravel will automatically set the fallback language if there is none specified
            App::setLocale(Config::get('app.fallback_locale'));
        }
        return $next($request);
    }
}
And of course we do not forget to update Kernel.php with our new Middleware (check the order I use)

protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'App\Http\Middleware\Language',
    'Illuminate\View\Middleware\ShareErrorsFromSession',
    'App\Http\Middleware\VerifyCsrfToken',
];

Friday, November 20, 2015

Configure remote connection to the MongoDB

Check whether you already have Mongodb process listening on the port
netstat -tulpen | grep 017

or
apt-get install procinfo
socklist - display

//Change authorization setting to disabled if you've enabled in
nano /etc/mongod.conf
sudo service mongod restart

Adding database authorisation (RoboMongo failed from Mongodb 3.0)
Creating a new user
cd /usr/bin/mongo to run mongodb shell

use admin
db.createUser( { user: "admin", pwd: "yourpassword", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } )

use <databasename>
db.createUser( { user: "accountUser", pwd: "password", roles: [ "readWrite", "dbAdmin" ] } ) 
db.auth("accountUser", "password")
db.getUsers()
db.dropUser(<username>)

//Now enable authorization again and restart the mongodb server


Filter IPTables to allow incoming and outgoing traffic from mongodb server

iptables -L to list the current rules
sudo iptables -L INPUT to see all the input values

iptables -A INPUT -s <ip-address> -p tcp --destination-port 27017 -m state --state NEW,ESTABLISHED -j ACCEPT 

iptables -A OUTPUT -d <ip-address> -p tcp --source-port 27017 -m state --state ESTABLISHED -j ACCEPT

iptables-save to save the rules

then type reboot to restart your terminal and now you'll be able to acces your mongodb



The first rule allows all incoming traffic from <ip-address> on port 27017, which allows the application server to connect to the mongod instance. The second rule, allows outgoing traffic from the mongod to reach the application server.


Lastest MongoDb package on debian is bind to 127.0.0.1, this address doesn’t allow the connection by remote hosts, to change it u must set bind to 0.0.0.0 for eg
root@debian:/var/www# nano /etc/mongodb.conf

bind_ip = 0.0.0.0
port = 27017
root@debian:/var/www/lithium# /etc/init.d/mongodb restart
Done! Remember to secure the connection by password in production mode.

Binding IP
bindip=<youridaddress> and then run the following command.. make sure that /db0 folder is existed

mongod --dbpath /var/lib/mongodb/ --repairpath /var/lib/mongodb/db0
/var/log/mongodb/mongodb.log to see log file if the service can't be started
rm /var/lib/mongodb/mongod.lock to remove the lock file
mongod --repair to repair the service
sudo service mongod restart

Extending Virtual memory in Ubuntu

$ sudo dd if=/dev/zero of=/mnt/swap.0 bs=1024 count=1048576 
$ sudo mkswap /mnt/swap.0 
$ sudo su 
swapon /mnt/swap.0
$ sudo swapon -s

Thursday, November 19, 2015

Fixing Autoload.php error while running projects from Github or others

Warning: require(/opt/lampp/htdocs/laravelprojects/mmdb_web/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /opt/lampp/htdocs/laravelprojects/mmdb_web/bootstrap/autoload.php on line 17

Fatal error: require(): Failed opening required '/opt/lampp/htdocs/laravelprojects/mmdb_web/bootstrap/../vendor/autoload.php' (include_path='.:/opt/lampp/lib/php') in /opt/lampp/htdocs/laravelprojects/mmdb_web/bootstrap/autoload.php on line 17


Use Composer install to update all the dependencies

Friday, November 13, 2015

Using S3 as a storage in Laravel 5

You can view all the regions available for your bucket folder in the following site to put region information in config/filesystem.php

http://www.bucketexplorer.com/documentation/amazon-s3--amazon-s3-buckets-and-regions.html

Add the following line to you s3 config file under confit/filesystem.php. This solve the Curl Error.

'http'    => [    'verify' => 'c:/xampp/php/cacert.pem']

Installing curl on windows

Using aws file storage in Laravel 5 needs curl installed.

Download the package from
http://curl.haxx.se/download.html#Win64

Extract it into c:/curl

Now open the system environment variables and add the c:/curl into the System path.

Fixing URL error 60: SSL certificate problem: unable to get local issuer certificate

Download curl certificate file at
http://curl.haxx.se/ca/cacert.pem

Copy and paste the dowloaded file into c:/xampp/php or whatever your php folder is located.

You certificate file should be located at /opt/lamp/php/cacert.pem if you're using lampstack on linux.

And point your S3 SSL path to the following directory for linux.

S3_SSL_PATH=/opt/lampstack-5.5.31-0/php/cacert.pem


Add the following line into the bottom of the php.ini file
[cURL]
curl.cainfo="C:\xampp\php\cacert.pem"