Friday, 20 May 2016

Laravel: How to update composer in laravel through command

How to update composer in laravel through command

sudo /usr/local/bin/composer self-update

Monday, 16 May 2016

Laravel 5 - Remove public from URL

For Laravel 5:

Follow steps below to remove "public" word from URL,

1) Rename the server.php in the your Laravel root folder to index.php

2) Copy .htaccess file from /public directory to your Laravel root folder.

3) Changing .htaccess file as follows for statics:


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
 


If there are any other static files needed just add the extension to the previous declared list

And now its done!!!

 

Tuesday, 29 March 2016

Laravel - Introduction and features

Laravel:

Laravel is a free, open-source PHP web framework created by Taylor Otwell.

Latest version of Laravel as of now is Laravel 5.1, released in June 2015.

Laravel is intended for the development with model–view–controller (MVC) architectural pattern.

In laravel, some of the features are a modular packaging system with a dedicated dependency manager, different ways for accessing relational databases, utilities that aid in application deployment and maintenance.

As of March 2015, Laravel is regarded as one of the most popular PHP frameworks, together with Symfony2, Nette, CodeIgniter, Yii2 and other frameworks.

The source code of Laravel is hosted on GitHub and licensed under the terms of MIT License.



Features of Laravel,

Bundles: provide a modular packaging system

Eloquent ORM (object-relational mapping): advanced PHP implementation of the active record pattern, providing at the same time internal methods for enforcing constraints on the relationships between database objects.

Query builder: provides a more direct database access alternative to the Eloquent ORM.

Application logic: integral part of developed applications, implemented either by using controllers or as part of the route declarations.

Reverse routing: defines a relationship between the links and routes, making it possible for later changes to routes to be automatically propagated into relevant links.

Class auto loading: provides automated loading of PHP classes without the need for manual maintenance of inclusion paths.

Blade templating engine: combines one or more templates with a data model to produce resulting views, doing that by transpiling the templates into cached PHP code for improved performance.

Unit testing is provided as an integral part of Laravel

Automatic pagination simplifies the task of implementing pagination

Monday, 21 March 2016

LOAD DATA INFILE statement in MySQL

Your script stops while fetch data from txt/csv file? Here is a MySQL feature to make this task faster. "LOAD DATA INFILE" statement::

The LOAD DATA INFILE statement reads rows from a text/CSV file into a table at a very high speed.

To write data from a table to a file, use SELECT ... INTO OUTFILE. To read the file back into a table, use LOAD DATA INFILE. The syntax of the FIELDS and LINES clauses is the same for both statements. Both clauses are optional, but FIELDS must precede LINES if both are specified.

SYNTAX to fetch data from CSV file:

$FILE_PATH = "test.csv";
$TABLE_NAME = "tablename";
$TABLE_FIELDS = "name,address";

'LOAD DATA LOCAL INFILE "$file_path"
            INTO TABLE "$table_name"
        FIELDS TERMINATED BY ","
        OPTIONALLY ENCLOSED BY "\"
        LINES TERMINATED BY "\n"
($TABLE_FIELDS)';

In the above example, your CSV file must contains 2 fields name and address and line terminated by "\n".

For more reference
http://dev.mysql.com/doc/refman/5.7/en/load-data.html

Friday, 18 March 2016

PHP strings: Difference between single and double quoted strings.

PHP strings can be specified in four ways.

1) Single quoted strings:

 
- This type of strings will display things completely "as is."
- Variables and escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\
- So yes, even single quoted strings are parsed.

2) Double quote strings:
- This type of strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated.
- An important point here is that you can use curly braces to isolate the name of the variable.
- For example let's say we have the variable $abc and we want to echo "The $abc are", That will look for the variable $abc.
  To get around this use echo "The {$abc} is" You can put the brace before or after the dollar sign.

Let us discuss an example for this,

$abc="test";
echo "test$abc"; // output = testtest

echo "test$abcaa"; // output = test


As $ is in Double quote, system will try to find remaining string as a variable. Like in first example $abc = "test"; and we request for "test@abc" so it will result as testtest. In second example system will try to find $abcaa variable and its not declared so it will consider as an empty string and result as test only.

So if we need to write test$abc as a string we need to use,


$abc="test";
echo 'test$abc'; // output = test$abc
echo 'test$abcaa'; // output = test$abcaa


Note: Single quotes being faster than double quotes in many situations.

3) Heredoc:
- String syntax works like double quoted strings.
- It starts with <<<. After this operator, an identifier is provided, then a newline.
- The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.

<?php
var_dump(array(<<<EOD
foobar!
EOD
)); exit;
?>


4) Nowdoc:

- PHP 5.3.0+
- String syntax works essentially like single quoted strings.
- The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.


<?php
class foo {
    public $bar = <<<'EOT'
bar
EOT;
}
?>

Visit http://php.net/manual/en/language.types.string.php for more information..

Wednesday, 16 March 2016

Difference between OR and || in PHP

Difference between OR and || in PHP

Based on behavior OR and || are same,

$a = 1;
$b = 1;

if($a==$b || $a=='5') {
   echo "here";
}  // OUTPUT: here


if($a==$b || $a=='5') {
   echo "here";
}  // OUTPUT: here



//////////////////////////////////////////////////

BUT, "OR" operator have lower precedence than ||

Because 'OR' have lower priority than '=' but '||' have higher priority. Same thing apply to "and" and && operator.

Let's check difference,

<?php
$bool = true && false;
var_dump($bool); // false, that's expected

$bool = true and false;
var_dump($bool); // true, ouch!
?>

Please refer below link for more detail,
http://php.net/manual/en/language.operators.precedence.php

So use only ("||" , "&&") operator instead of ("or","and") operator.

Enjoy your coding!!!

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

Difference between == (equality or Double Equals) and === (identity or Triple Equals) in PHP

== compares the values of variables for equality, type casting as necessary. 


=== checks and returns true only if the two variables are of the same type AND have the same value.


Examples:
a === a // returns true
a == a  // returns true

1 === "1" // returns false because 1 is an integer, "1" is a string
1 == "1"  // returns true because "1" gets casted to an integer, which is 1


"abc" == "abc" // returns true
"abc" === "abc" // returns true because both operands are strings and have the same value


For the security reasons, it is better to use identity operator whenever possible because it compares variable type also.

Enjoy your coding!!!