Showing posts with label PHP Extensions. Show all posts
Showing posts with label PHP Extensions. Show all posts

Sunday, March 22, 2020

Laravel App Connect and Emit to Socket.io

Hello,

Recently in one of the project I was working on we have socket.io server created using NodeJS and there were couple of Angular applications and android applications connects with it. However there was a requirement to connect with socket from Laravel app and emit the event.

Actually there was a form in Laravel app on submit of Form we have to notify the socket and socket further notify all the clients connected to it. In this blog I am going to mention how we solved this.

We all know Laravel by default comes with integration of Pusher we could have used it. But since Socket.io is free so we consider to use Socket.io.

Let's go step by step and understand how to connect with Socket.io

Step 1

Create new folder socket in your app directory of Laravel app.

Step 2

Copy following class in to app/Socket folder and rename it to SocketIO.php

https://github.com/psinetron/PHP_SocketIO_Client

There are many PHP socket IO client is available but we used above one. You can choose either one of your choice.

Step 3

Connect and emit to socket. In your controller file where you want to emit the event. Add following code.

$socketIO = new SocketIO();
$returnValue = $socketIO->send('YOUR_URL', YOUR_PORT, 'YOUR_EVENT',json_encode(YOUR_PHP_ARRAY))

For this add following line to top of your controller.

use App\socket\SocketIO;

That's it and now you can connect with socket and emit event to it.

Sunday, May 5, 2019

Laravel Mail Queue Get Receiver User

Hello,

Recently in one my Laravel project faced an issue where in Mail we have to change content dynamically based on receiver. As we know that when we use Mailable class and mail queue, mail will not sent immediately but it will be processed with Mail Queue. Here we don't have Auth Session or any other information to get receiver user.

To solve this I used following trick.

In mailable class if you check $this variable it will give all such information like this.

from-> => "norply@xyz.com"
address-> : Array[
    0 => Array[
          "address" => "hdave10@gmail.com"
    ]
]

From here you can access email address like this

$emailOfReceiver = $this->to[0]["address"];

And using this you can find user object from Auth\User model or your own model which you used for user information in your laravel project. For my case I was using EmployeeMaster model to get additional information.

Following line I have used in one my Mailable class.

$employee = EmployeeMaster::where("emp_email",$this->to[0]["address"])->first();

Now with this I can write my logic to dynamically change the content of mail.

Hope this helps you.

Thursday, November 1, 2018

Laravel : Better Way To Deal with Trying to Get Property Of Non Object

Hello Everyone,

After a gap of almost 3 months I am again writing a blog. In laravel project we sometimes face issues like we try to access model but it's not available due to various reason like
  • id we passed is wrong
  • we manually deleted records from database
  • wrong value saved in database.
  • wrong model you are trying to access with wrong value.
When we get this error, Laravel throws an ErrorException with message "Trying to get property of non object."

However, end user does not understand this error so we have to show them proper message on what's missing. We can not display generic error page and for each line of code we can not put the check so better to check it at single place. Laravel gives this functionality in Exception Handler. Here the trick is to get model which throws this error and get name of Model and display proper message. 

Laravel has class ModelNotFoundException which is thrown when you use findOrFail function. However we developers sometime do not use this function and use find function. 

When find function is used, it throws ErrorException which does not contain Model information. However we can modify Laravel source code for this. 

Open following file 

vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php

Here find public function find($id, $columns = ['*'])

and modify it as following.

        if (is_array($id) || $id instanceof Arrayable) {
            return $this->findMany($id, $columns);
        }
        $object = $this->whereKey($id)->first($columns);
        if($object == null){
            throw (new ModelNotFoundException)->setModel(
                get_class($this->model), $id
            );
        }else{
            return $this->whereKey($id)->first($columns);
        }

PLEASE NOTE : I DO NOT RECOMMEND TO MODIFY SOURCE FILE SO BETTER WAY IS TO USE FINDORFAIL FUNCTION.

Now if you use find or findOrFail function it will throw ModelNotFoundException. Now open app/Exception/Handler.php file and find out function public function render($request, Exception $exception)

Add following code at start of the function.

       if(get_class($exception) == 'Illuminate\Database\Eloquent\ModelNotFoundException'){
            $modelName = explode("\\",$exception->getModel());
            $modelName = $modelName[count($modelName)-1];
            $message = "You are trying to access ".$modelName." which does not exist.";
                return Response::make(view('Frontend.error')->with(array(
                    "message"=>$message
            )));
        }

That's it and now it will display proper message like

You are trying to access User which does not exist.
You are trying to access Post which does not exist.

Hope this helps you.

Friday, May 26, 2017

PHP strtotime() does not work with dd/mm/yyyy Format

Hello,

This is quick blog regarding PHP strototime function. In our recent project we were importing data from CSV where we faced two issues.

1) If we try to import date like 01/05/2016 considering dd/mm/yyyy format, it saves date in database as 2015-01-05

2) If we try to import date like 31/08/2016 considering dd/mm/yyyy format, it saves data in database as 1970-01-01

In short month and day is swiped while saving. So in case of date like 31/08/2016,  31 is assumed as month and there is no such month in calendar so it gave error and returns date like 1970-01-01

This is first time we faced such issue. I was not sure what's the exact issue but I assumed it has something to do with separator. So we tried same dates with  - instead of / and it worked. So to solve this temporary, we had following solution.

$date = '31/08/2016';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

So we replace / with - and it worked but solving problem is not enough, we shall go in deep to check why we had this problem so curiously I looked into function documentation of strtotime and found out following.

"Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. "

So indeed the problem here was with the separator.

Hope this helps you.

Saturday, October 1, 2016

Laravel 5.x Store Uploaded Files to Amazon S3

Recently in one of my Laravel 5.2 project, we used Amazon s3 to store static files and user uploaded files. So here in this blog I am going to explain how to do this.

1) First of all install following plugin through composer.

composer require league/flysystem-aws-s3-v3 ~1.0

2) Now add your S3 credentials to your environment (.env file)

S3_KEY=YOUR_KEY
S3_SECRET=YOUR_SECRET
S3_REGION=YOUR_REGION
S3_BUCKET=YOUR_BUCKET

3) Open config.filesystems.php and configure s3 driver as follow.

's3' => [
            'driver' => 's3',
            'key'    => env('S3_KEY'),
            'secret' => env('S3_SECRET'),
            'region' => env('S3_REGION'),y.
            'bucket' => env('S3_BUCKET'),
        ]

4) Now open controller where you have code to manage uploaded file and add following dependency.

use Illuminate\Contracts\Filesystem\Filesystem;

5) Use following code to store your file to S3.

$image = $request->file('user_uploaded_image');
$imageFileName = time() . '.' . $image->getClientOriginalExtension();
$s3 = \Storage::disk('s3');
$filePath = '/mypath'/.$imageFileName;
$s3->put($filePath, file_get_contents($image), 'public');
$uploadedFileS3Path = 'https://'.env('S3_BUCKET').'/'.$filePath;

Please note in my case I configured S3 bucket to be direct URL of uploaded file. In case your case if you have not configured it then your path will be as follow.

$uploadedFileS3Path = 'http://s3-'.env('S3_REGION').'.amazonaws.com/'.env('S3_BUCKET').'/'.$filePath;

This path you can use to display file and you can store it to database as well.


Special Case 

If you are using it in localhost using XAMPP in your windows system. You have to do following few steps.

Download certificate pem file from this link. https://curl.haxx.se/ca/cacert.pem and store it to some where in your C drive or whatever drive you have your xampp installed.

For example I put it in  C:\code\cacert.pem

Now open your php.ini file and bottom of the file add following line.

curl.cainfo = C:\code\cacert.pem

That's it and it will work in your local environment too. Hope this helps you.

Saturday, September 10, 2016

Laravel 5.3 Run Artisan Command Through Routes

Recently for one my project we installed Laravel 5.3 on shared server where we were not having SSH or route access.

As we know general practice with Laravel is to create database tables with migrations and for that we have to run artisan commands through terminal and SSH.

But in our case we were not having SSH access, we have only FTP and database access so we have to figure out a way to run this commands through web. In this blog I am going to explain how to do this.

After reading Laravel 5.3 document I came to know that Laravel gives you Artisan class to run artisan command. So what I did is I created two routes, one for creating migration and other to run migration.

To create migration

Route::get('/create-migration-command', function () {
    Artisan::call('make:migration', ['name' => 'migration_name']);
});

As you can see here the command is make:migration and parameter passed is name of migration. 

Virtually it is equivalent to following SSH command.

php artisan make:migration migration_name

This will generate migration file in database folder, you can open it through FTP and add necessary code of migration.

Run Migration

Route::get('/run-migration-command', function () {
    Artisan::call('migrate', []);
});

Virtually it is equivalent to following SSH command.

php artisan migrate

Hope this helps you.

Laravel 5.3 Internal Server Error After Installation

Recently for one of my project I have installed Laravel 5.3 on server but after installation when I tried to run it in web it was showing 500 Internal Server Error so I had to spend few hours in resolving it so here in this blog I am going to explain what was the issue and how I solved it.

1) Find out the Error.

I looked into to server logs from cPanel and found out following error.

SoftException in Application.cpp:429: Mismatch between target UID (501) and UID (99) of file "/path/larravel/public/index.php"


After searching for sometime about this error I figured out that the apache or web user was not owner for the files because I created laravel project through SSH so the logged in SSH user was the owner of the files and folders.

So when apache or web user tried to access the files it was not working. So the solution was to change the ownership of files and folders to apache user.

2) Find out the username of apache user.

In most cases user name of apache user should be www-data or apache or apache2 but in my case since I was using VPS the username was different. So run following command in terminal through SSH

ls -l

It will give you file list with user and group owner. That's your web username. Now change the ownership of folder to this username.


chown -R username:username laravel

That's it. Now run your laravel project and it shall display following screen.

That means your laravel project is working fine on the server. Hope this helps you.

Thursday, April 14, 2016

Laravel Share Some Variable with All Views

Hello,

Recently in one of my laravel project we have a requirement create side menu in app with some dynamic stats numbers like

Users   (12)
Managers  (10)
Questions  (25)

This menu is included in all the views and to set dynamic values in that we have to share this numbers globally. In this blog I am going to mention how to so this.

1) Create a BaseController class and extend it with controller and add a constructor.

class BaseController extends Controller {
       public function __construct() {
     
       }
}

2) Inside that constructor get all the numbers and share it with view

$users = DB::table('users')->count();
$managers = DB::table('managers')->count();
$questions = DB::table('questions')->count();

View::share('users',$users);
View::share('managers',$managers);
View::share('questions',$questions);

after adding this code your base controller should look like following.

class BaseController extends Controller {
       public function __construct() {
             $users = DB::table('users')->count();
             $managers = DB::table('managers')->count();
             $questions = DB::table('questions')->count();

            View::share('users',$users);
            View::share('managers',$managers);
            View::share('questions',$questions);
       }
}


3) Now extend this base controller to all other controllers.

class MyConttoller extends BaseController {

}

That's it now any view which you render from controller will have those variables by default. Now you just have to use those variables in views.

<ul>
       <li> Users  ({{$users}}) <li/>
       <li> Managers  ({{$managers}}) <li/>
       <li> Questions  ({{$questions}}) <li/>
<ul/>

Hope this helps you.


Wednesday, July 15, 2015

MAMP - Install MSSQL Extension

Recently in one of my project I was trying to connect to MS SQL server from PHP and it was showing error  "Call to undefined function mssql_connect()". That means you are missing MSSQL extension and you have to install it. In this blog I am going to explain how to do this.

First of all you will need FreeTds. FreeTds is the library for linux and unix to communicate with MS SQL server. You can download latest stable release from this link

ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz

After downloading it, extract it and go to that directory in terminal and run following command.

$ sudo ./configure --prefix=/private/etc/freetds --with-tdsver=8.0 --sysconfdir=/private/etc/freetds/conf
$ sudo make
$ sudo make install

This will install FreeTds on your system. Now we have to compile MSSQL extension for your PHP version. For that first check which PHP version are you using in MAMP. That you can check in Server -> PHP tab


Check your version and download source code of that PHP version.


Extract it and go to this directory in terminal.

Now run following commands in terminal

$ ./configure --with-php-config=/usr/bin/php-config
$ make

Now go to MSSQL extension directory in PHP source code.

$ cd ext/mssql/
$ /usr/bin/phpize
$ ./configure --with-php-config=/usr/bin/php-config --with-mssql=/private/etc/freetds
$ make

Above command will generate MSSQL module so now go to module directory.

$ cd modules/

Now we have to copy this generated module to MAMP's current PHP extension directory. For that open the ini file of your PHP version. In my case I was using PHP 5.5.3 so my ini file was located at.

/Applications/MAMP/bin/php/php5.5.3/conf/php.ini

Open this file and search for "extension_dir" and you will find following line.

extension_dir = "/Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/"

so this is your extension directory. We have to copy module here. So now go to terminal and run following command.

$ cp mssql.so /Applications/MAMP/bin/php/php5.5.3/lib/php/extensions/no-debug-non-zts-20121212/

This will copy mssql.so in your extensions directory. Now we have to add this extension in php.ini file.

Now in php.ini file find for the word "; Extensions" below that there are list of extensions. Add following line to the end of the list.

extension=mssql.so

Save the file. Restart your MAMP and run the phpinfo(). You must see following section there.


But wait it won't be there. That means there is something wrong. After wasting almost an hour I figure out that MAMP is actually using different ini file.  So we have to update that. For that go to 

/Applications/

Right click on your MAMP or MAMP Pro icon (if you are using MAMP Pro) and select "Show Package Contents" Now go to Contents/Resources  and open ini file of your PHP version. Now in that file find for the word "; Extensions" below that there are list of extensions. Add following line to the end of the list.

extension=mssql.so

Save the file. Restart your MAMP and run the phpinfo(). You must see following section there.


That's it and now you can connect to MSSQL server with following code.

$con = mssql_connect('MYSQLSERVER', 'username', 'password')
or die('could not connect to server.");

I hope this helps you and saves your time.