Laravel Composer using the wrong PHP version

I installed WAMP for offline development, then Composer and then Laravel. Everything was OK until I used Composer to download some Laravel package and I got this kind of error:

This package requires php >=7.0.0 but your PHP version (5.6.25) does not satisfy that requirement

Number don’t matter but I needed newer PHP. OK, I changed PHP version on WAMP, restarted it..and Laravel composer gave me the same error!

Long story short, after an hour of googling and trying all the suggested solutions like editing composer.json and this and that, nothing worked!

The solution is simple: reinstall Composer! D’oh!

When you run setup you can select the PHP version you want Composer to use (of course you have to have this PHP version installed on your PC or server) and the Composer will use it from then on:

 

laravel composer php version

After doing this this I was able to install the package. To be honest, I was quite upset that so many people offered some complicated solutions (that didn’t even work for me) on various forums and wasted my time when the solution was so simple.

 

 

 

Cmder displays {lamb} and {git} instead of λ and current dir

I recently installed cmder for Windows and when I ran it it was displaying {lamb} and {git} instead of λ and current dir.

Cmder displays {lamb} and {git} instead of λ and current dir

It came with the 0.4.2 version of Clink , which apparently is incompatible or doesnt work best with Windows 10.

Solution is very simple:

1. Go to https://mridgers.github.io/clink/ and download ZIP version of  Clink (.exe is probably OK too but I downloaded ZIP). The new version I downloaded was 0.4.9.

2.  Go to the cmder_mini/vendor folder (where you installed cmder) and rename clink folder to something like clink_original (or you can delete it if you like but I prefer to backup first if something goes wrong) and recreate a folder named clink  and simply extract all files from the downloaded ZIP to this folder.

3. Close (if you had it open) and restart cmder and this is what you should see:

cmder clink

Global variable set in the template’s header.php is empty in footer.php

If you set up a global variable in the header you would expect that it would be available everywhere, right? That is the definition of the word ‘global;, right? Well, it doesn’t work like this and the reason is because in WordPress header and footer are called via functions so the variable is only accessible in scope of each function.

One of the solutions is simple – use global variable before the declaration AND before echoing.

For example, add this to header.php:

<?php global $var; $var= "something"; ?>

and then in the footer.php you do this :

<?php global $var; echo $var; ?>

and it will echo the content of your $var variable.

Be advised though that some people say using global variables is not the best practice.

Get WordPress Post ID Outside the Loop

It is easy to get a post ID in WordPress. To echo it on screen we write:

 <?php the_ID(); ?>

To store the ID into variable we use something like this :

 $id = get_the_ID();

There is one caveat though: if you read the description on WordPress codex it says this in both cases: This tag must be within The Loop.

So if you ever wanted to retrieve or display the WordPress post ID in footer or header or outside of The Loop you might end up being disappointed because the ID returned is not the correct one (usually we always get the same id, most probably the last one?)

Now I am not going to go into technical details or why is that so here is the solution that worked for me. You

$post = $wp_query->post;
echo $post->ID;

There are some other ways with global variables and some other but it seem it doesn’t work for everyone. This one worked on the first try. Let me know in comments if it doesn’t work for you.

Turn On Error Reporting on WordPress Blank / White Screen of Death

There is nothing worse than getting blank screen when developing for WordPres – also ‘officially’ known as The White Screen of Death – because you have no idea what went wrong. No errors, nothing useful, just blank screen. It’s even worse when it just appears out of nowhere. This happened to me few times in the past and then just recently. Woke up in the morning and the entire site (not this one) displayed just blank white page.

First thing you should do is rename the plugins folder and see if it works. If yes then the problem is in one of the plugins so if your admin works start disabling plugins one by one. If admin doesn’t work then try renaming plugin folders via FTP. If this doesn’t help then rename the themes folder and see if the problem was caused by one of your theme.

This didn’t help in my case. So I went to WordPress site (link below) and insert the code they recommended! It STILL didn’t work and there were no errors displayed! So finally I modified the code a little and finally the error appeared! I don’k know why are they posting a solution that doesn’t display the errors.

Here is the code that started showing errors and I finally able to fix it. Still the mistery remains why did the blank screen start appearing when it was working just fine yesterday when I went to bed. Ah, the ‘joys’ of developing.

Open the wp-config.php file in the root directory of your worpress and insert this just above the /* That's all, stop editing! Happy blogging. */ comment. Refresh the WordPress and hopefully you will see PHP error telling you what went wrong.

// Enable WP_DEBUG mode
define('WP_DEBUG', true);

// Enable Debug logging to the /wp-content/debug.log file
define('WP_DEBUG_LOG', true);

// Enable display of errors and warnings 
define('WP_DEBUG_DISPLAY', true);
error_reporting(E_ALL);
@ini_set("display_errors", 1);

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define('SCRIPT_DEBUG', true);

After you fix the error don’t forget to remove or at least comment the added code.

In case it didn’t work, here are some more steps to try to fix it:

Common WordPress Errors and Debugging in WordPress

Redirect all requests for the domain root to a specific page

This is for WordPress but can be used in any site because it uses .htaccess.

I needed to redirect all visitors that visit the root URL (for example elcoderino.com) to specific subpage (eg elcoderino.com/subpage). WordPress allows you to create a static front page or select a custom page that will replace default front page content but in my case it just didn’t work. Maybe it was combination of Advanced Custom Fields or custom post types but it just kept messing up the front page.

So the solution is to add this to your .htaccess file:

RewriteEngine On
RewriteRule ^$ /subpage/ [R=301,L]  

if your .htaccess file already contains RewriteEngine On then simply just add RewriteRule ^$ /subpage/ [R=301,L] below it.

Enable qTranslate for WordPress 4

Seems like qTranslate is not really updated anymore or at least it is updated very slowly. So when I updated WordPress to new version I got this message and qTranslate editor in admin was disabled.

The qTranslate Editor has disabled itself because it hasn’t been tested with your WordPress version yet. This is done to prevent WordPress from malfunctioning. You can reenable it by clicking here (may cause data loss! Use at own risk!). To remove this message permanently, please update qTranslate to the corresponding version.

I guess the easiest way is to just click on the link and it will probably enable itself. I googled it and it seems some people might have problems with that. So the solution that worked for most people is this:

First, open wp-content/plugins/qtranslate/qtranslate.php file and change line 90 from

define('QT_SUPPORTED_WP_VERSION', '3.7.1');

to

define('QT_SUPPORTED_WP_VERSION', '4.0');

just save it and that’s it.

Of course if WordPress is updated to new version qTranslate will disable itself again. What you can do is enable it for all version by dynamically inserting current WordPress version, like this

define('QT_SUPPORTED_WP_VERSION', get_bloginfo('version'));

qTranslate will now work for all (future) versions of WordPress.

Hint: You can find out your WordPress version on the bottom right in the admin or by opening a file /wp-includes/version.php and checking the $wp_version variable on the top.

Note: Do this on your own risk. It seems to work for most people but beware you might loose your data if something goes wrong. The safest way is to wait for author of qTranslate to update it but it seems this is not happening.

Laravel 4 with Twitter Bootstrap

One of the first thing many, including me, want to do after installing Laravel 4 is to add Twitter Bootstrap. If you go searching for this on Google you will get all kids of results that may confuse beginner. “Use Bower”, “Install it as a external package then use Basset” and several more. I think some are a little too complicated for a beginner and it doesn’t need to be. I know adding a line to composer.json is easy but this will usually (depending on whcih package you chose) download gazillion of files to your vendor folder.

Here is what to do for a simple website – you don’t need to treat it as a external package that needs to be added via composer, bower or be used with help of Basset etc. Sure, it’s easier to update it via composer but it is not that difficult to update it manually too – if you indeed need an update.

So here are few easy ways:

Use Bootstrap CDN links

Simply include 3 links, which will fetch your twitter bootstrap files from MaxCDN site. This is the easiest and fastest way. Let’s assume that you have a master blade template called default.blade.php. Place this code inside your HTML head section (anywhere):

<head>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>

</head>

That’s it, you should now be able to use Twitter Bootstrap goodness.

Download Bootstrap and put it in your public folder

I like to host my own files and not depend on third party so I usually do this. Here we don’t treat Twitter Bootstrap as a Laravel package.We will bypass Composer so we manually put Twitter Bootstrap files into our public assets folder.

Go to getbootstrap.com, download Twitter Bootstrap ZIP file and extract the content of ZIP file (pull entire dist folder) to your /public/ folder. I usually rename dist folder to tb.

You will then end up with css,fonts and js folders inside /public/tb/ folder.

Now we follow the same procedure like before except that we will link to local files we just downloaded. I will use Laravel’s HTML helper to create links. {{ and }} is a Laravel Blade syntax.

<head>

{{ HTML::style('tb/css/bootstrap.css') }}
{{ HTML::style('tb/css/bootstrap-theme.min.css') }}
{{ HTML::script('tb/js/bootstrap.min.js') }}

</head>

As you can guess the first two lines will create CSS style links and the third one will create script link. If you view the source of generated page you will see that Laravel’s HTML helper created something like this (of course http://yoursite.dev will be replaced by your site) :

<head>

<link media="all" type="text/css" rel="stylesheet" href="http://yoursite.dev/tb/css/bootstrap.css">
<link media="all" type="text/css" rel="stylesheet" href="http://yoursite.dev/tb/css/bootstrap-theme.min.css">
<script src="http://yoursite.dev/tb/js/bootstrap.min.js"></script>

</head>

There are other ways (Bower, Composer, Basset…), which I might show in another post (or maybe I’ll update this post later) but these two are the easiest, especially for those just starting with Laravel 4.

Enjoy your Twitter Bootstrap powered Laravel 4 website 🙂

Commands out of sync; you can’t run this command now

Sometimes the solutions to what seems to be complex problems are easy and often something completely unrelated.

I woke up to one of my websites not working although I did not touch the code for weeks. First thing that I got was Firefox telling me this :

The page isn’t redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete

After ruling out .htaccess and other problems I came to conclusion that it has something to do with a database querying.

So I went to phpmyadmin and tried to manually run the query. To my surprise I got another problem:

Commands out of sync; you can’t run this command now

Great, so suddenly the nested MySQL command I have been using for years stopped working. If you Google for this you will get tons of sites telling you that MySQL has problem with stored procedures (I’m not even using them), that we need to switch to mysqli or PDO extensions, that we are calling client functions in the wrong order etc. However this did not explain why did my code suddenly stop working despite me not touching anything. Maybe database got too large?

To troubleshoot this I wanted to make a copy of a specific table in question and lo and behold – when I copied it via phpmyadmin I got error saying something like “This table is corrupt so it cant be copied”. I checked the table and there it was – a corrupt table! I ran a repair table command, refreshed a site and voila, it worked!

So as you can see the solution was something completely unrelated to error messages and what most websites suggested it was. That does not mean that most people with this errors have the same problem like I did. But it is a reminder that sometimes we should try a simple solutions like checking if database or table became corrupt before spending few hours into troubleshooting something that is working fine anyway 🙂

.

‘git’ is not recognized as an internal or external command

If you installed Git from windows.github.com you might get this error while running ‘git’ from windows command prompt(cmd):

‘git’ is not recognized as an internal or external command, operable program or batch file.

What you need to do is add Git path to Windows Environment Variables.

First, search for git.exe with windows search. You will find it in directory similar to this one:

C:\Users\user\AppData\Local\GitHub\PortableGit_76ds5d7f65adsf76as5f7as6f5asdf\bin

Add this to Windows Environment Variables :

  • right mouse-click “My Computer” and select “Properties”
  • if on Windows 7 then click on “Advanced system settings”,
  • open “Advanced” tab and click “Environment Variables”,
  • highlight/click on the “Path” variable under “System variables” and click “Edit”,
  • add your specific path to front of “Variable value” field or at the end, separated by a semicolon from the existing entry. Do not add a space between ; and last entry.Also, do not add ; after the last entry

Let say, just for example, that the Git.exe is in C:\Users\something\somefolder\bin folder. So when you edit your path variable it should look like this (thanks to my super Photoshop skills you can see that we are on Advanced tab, that we are editing Path variable and that I excel at making watermarks):

edit system variable git

Click Ok few times and try running Git again.