Archive for the ‘Laravel’ Category

Which framework to use in PHP

Thursday, April 20th, 2017

While doing my freelancer projects I keep on monitoring what frameworks are out there. I have been working with Java for many years but have landed on PHP as smaller projects are much faster and easier to get off the ground.

The more I observe Google Trends the more obvious it gets that Laravel is coming here to stay. Only framework that has trouble dying is Symfony and it could also be because of Laravel. Not much else is needed to say that only one picture and all is clear which PHP framework you must learn if you want to be among the best.

Laravel 5 unit testing Form and TokenMismatchException in Middleware

Friday, December 26th, 2014

While unit testing in Laravel 4 you were able to turn off filters including CSRF check in testing environment. However in the Laravel 5 CSRF check has been move into the middleware and I have not yet found the way to turn CSRF check off easily.

As TokenMismatchException will happen when testing controllers that are processing forms then I needed a hack to run the tests properly. I made a change to Illuminate\Foundation\Http\Middleware\VerifyCsrfToken . I added bypassing token check if environment is testing. You can do it also if you change the method handle to be like this

public function handle($request, Closure $next)
{
 if (env("APP_ENV")=="testing" || $this->isReading($request) || $this->tokensMatch($request))
 {
   return $this->addCookieToResponse($request, $next($request));
 }
 throw new TokenMismatchException;
}

Laravel 5 Seeder not found

Sunday, December 14th, 2014

I tried to create new seeder in Laravel 5 and the seeder class was not found. I douoble checked like 10 times anddid not find what was wrong. Finally I tried “php artisan optimize” and the Seeder start working again!

You can fix it also if you happen to get exception like this:

 exception 'ReflectionException' with message 'Class RouteSeeder does not exist' in /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php:745
 Stack trace:
 #0 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(745): ReflectionClass->__construct('RouteSeeder')
 #1 /var/www/vendor/laravel/framework/src/Illuminate/Container/Container.php(650): Illuminate\Container\Container->build('RouteSeeder', Array)
 #2 /var/www/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(505): Illuminate\Container\Container->make('RouteSeeder', Array)
 #3 /var/www/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(55): Illuminate\Foundation\Application->make('RouteSeeder')
 #4 /var/www/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(37): Illuminate\Database\Seeder->resolve('RouteSeeder')
 #5 /var/www/resources/database/seeds/DatabaseSeeder.php(17): Illuminate\Database\Seeder->call('RouteSeeder')
 #6 /var/www/vendor/laravel/framework/src/Illuminate/Database/Console/SeedCommand.php(57): DatabaseSeeder->run()
 #7 [internal function]: Illuminate\Database\Console\SeedCommand->fire()

Namespaces and Laravel 5 with Eloquent Model-s

Sunday, December 14th, 2014

Recently I tried out the Laravel 5.  A lot has changed, overall it is using even more cutting edge technologies and best practices. On the other hand its learning curve is not so good anymore and this makes it harder for nwebie developers to start with it.

Major change is that you have to deal with namespaces on your own. Also the extends Eloquent is now replaced with extends Model (Illuminate\Database\Eloquent\Model) .  If your model name is Port then you need to import it in your controllers etc via “use App\Port;”

Example class looks now like:

 <?php namespace App;
 use Illuminate\Database\Eloquent\Model;
 class Port extends Model {
 }

These are exceptions that might happen for you if you do not handle it properly:

Class ‘Illuminate\Database\Eloquent’ not found

FatalErrorException in Port.php line 5:Class ‘Eloquent’ not found

HandleExceptions->fatalExceptionFromError(array(‘type’ => ‘1’, ‘message’ => ‘Class ‘Illuminate\Database\Eloquent’ not found’

FatalErrorException in AdminController.php line 12: Class ‘App\Http\Controllers\Port’ not found

PHP locale not working in Ubuntu

Tuesday, September 24th, 2013

I was working with Laravel 4 and trying to format Carbon object dates according to locale. However App::setLocale(‘et_EE’) did not have any effect. Going lower level and setting setlocale(LC_ALL, ‘et_EE’) did not change locale either. Carbon uses under formatLocalized method PHP strftime so issue relates to this also. Some have suggested to use

utf8_encode(strftime("%A, %d de %B",time()));

But it is not perfect solution either.

It turns out that OS must support the locale. You can check all available locales in your machine with

locale -a

If your webapp supported locales are missing then add these with command

locale-gen en_US et_EE

Removing locales is easy also, just add –purge to the command, which removes all other locales and keeps only the ones you specify.

locale-gen --purge en_US et_EE

I had another specific issue with locale et_EE, by default it is encoded in ISO-8859-15 ( et_EE.iso885915 ). If all application is in UTF-8 then dates will have special chares ÜÕÖÄ corrupted. To force UTF-8 only ( et_EE => et_EE.utf8 ) I had to make changes in this file

/usr/share/i18n/SUPPORTED

and edit there line et_EE ISO-8859-15 to et_EE UTF-8

 

 

IE and Facebook iFrame app session cookies problem

Sunday, February 10th, 2013

I wrote and Facebook app that uses sessions. I worked fine in my machine where I use mostly Firefox and Chrome. Suddenly I discovered that after resetting security settings to medium in IE 9 the app suddenly did not work anymore.

Each time I refreshed the new session key was created because requests did not send the cookies. Apparently IE decided to delete the cookies when I was running my Facebook app as page tab inside iFrame. Fortunately I have seen this before and immediately recognized the problem when I saw session key changing each request. It is called P3P headers that someone invented long ago but then understood that these are useless.

To fix it you need to add following line in your PHP code.

header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');

If you happen to use Laravel or other framework then add above code to Before filter.

See for other languages http://www.admon.org/how-to-implement-p3p-http-headers-for-cross-site-cookies/

 

 

Facebook signed _request missing

Saturday, February 9th, 2013

I have Facebook app as a page tab and sometimes it did not get the signed_request.

I saw with Firebug that POST to my app is made and signed_request is there but this call made redirect to GET instead. This was always reproducible with one Facebook account but with another account it worked just fine. I used Laravel framework and tried adding debugging code to index.php where all requests are routed through and also created separate index.php so that .htaccess would be bypassed. It turned out that POST request is not reaching the code at all.

What I found out is that even though I run all the services over HTTPS and in Facebook app config I have only HTTPS links then Facebook still forces app to be loaded over HTTP. I have apache2 virtual host configuration so that all HTTP requests are redirected to HTTPS which loses all the POST data and that is why the guaranteed signed_request never reached my app.

I resolved it by allowing HTTP but any navigation in the website was changed to HTTPS again.

But really, why java is enterprise and PHP is not.

Monday, January 21st, 2013

I have a lot of experience with Java and until this was only language I knew well I could not say out the real answer. Now recently I have been working with PHP + Laravel and finally I know the answer.

PHP by itself is not bad. Programmer can quickly hack all kinds of great things, deploy it just by copying new files to webroot and done. In that sence PHP is easy to learn and use, it is flexible and in that sense great tool for beginners.

Java, however, is more cumbersome. It requires more setup and compiling. All the data types must be strict, all kinds of places have to try catch Exceptions. It feel like a lot of extra work for nothing.

Now when we look how enterprise projects looks like. There are thousands or even millions lines of code, huge number of files, classes etc. Stack traces traces are tens and hundreds of rows. Maintaining such a projects must not leave any room for error. Making simple typecasting mistake or typing method for object that is not supported is not an option.

So the answer is:  Java is more refained, it forces you to think harder and make it more right on first try. IDE-s allow you to traverse code much more easily, leaving out all possible confusion with data types in inheritance.

Laravel Redirect:to is ignored and not working

Thursday, December 6th, 2012

I was implementing Paypal with Laravel framework and when it came to performing redirect then it did not happen no matter what I tried.

I tried in controller something like

header('Location: https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=' . urlencode($token));

and

Redirect::to('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=' . urlencode($token));

Turned out that manual is best friend like always and you have to use Redirect::to only in return statement like this

return Redirect::to('https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=' . urlencode($token));

Laravel and PayPal “You do not have permissions to make this API call”

Wednesday, December 5th, 2012

I discovered great new PHP framework Laravel. When trying it out I implemented Laravel Paypal bundle which was supposed to make the integrating it very simple. (Un)fortunately it was not so out of the box. Firstly the Paypal bundle is just a wrapper to make it easier using it in Laravel. You still have to use implementation manual like http://coding.smashingmagazine.com/2011/09/05/getting-started-with-the-paypal-api/. Secondly no matter what call I tried I always got response like

( [TIMESTAMP] => 2012-12-05T20:02:14Z [CORRELATIONID] => 375ff5175b9d [ACK] => Failure [VERSION] => 74.0 [BUILD] => 4181146 [L_ERRORCODE0] => 10002 [L_SHORTMESSAGE0] => Authentication/Authorization Failed [L_LONGMESSAGE0] => You do not have permissions to make this API call [L_SEVERITYCODE0] => Error )

I renamed configuration file paypal-sample.php to paypal.php and added correct Paypal username, password and signature into there. File stayed into Laravel bundles/paypal/config folder where artisan installed it.

Turns out this was mistake, configuration was read from wrong location and no credentials was sent to Paypal. When reading github manual closer then it says clearly the config file must be move into more logical place application/config/paypal.php

Another comment I have for Laravel Paypal bundle is that you need to use method names so that you add replace capital letters with underscore and lowercase letter. For example if you want to send SetExpressCheckout then you must use method Paypal::set_express_checkout(). Paypal bundle will convert it back to original form.