Laravel 5, new Helper Functions

helper functions

Helper Functions can provide you, sometimes, a nice and short way of coding. Laravel 5 offers some new helper functions and here is the list:

Routing

Handle HTTP GET requests by registering a route: Route::get The same logic is used for deleting and posting:

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]
get(‘/’, ‘HomeController@index’);

// Route::post()
post(‘login’, function() { … });

// Route::delete()
delete(‘post/{id}’, function() { … });

// Route::put()
put(‘post/{id}’, function() { … });

[/cc]

 

Controllers

You can use a helper inside a Controller to return values used in it:

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]
// View::make()
view(‘hello’);

// Redirect::to()
redirect(‘home’);
redirect(‘login’)->with(‘message’, ‘Login Failed’);

[/cc]

 

Logging

Put an information message to the log file and is used for a Log::info

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

info(‘Email sent successfully!’,[‘context’=>’more details here’]);

[/cc]

 

Configurations

Config helper function returns a value related to the $key passed as an argument

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

echo config(‘app.debug’);
//we can pass a default value as a second argument.
echo config(‘app.url’,’default’);

[/cc]

Integrate PHP Debugbar into Laravel 4

Why inlcuding Php Debugbar in your projects?

Php Debugbar is a very useful package to integrate in your Laravel Projects. It can be used to monitor your assets like – AJAX request, Cache, Doctrine Redirects etc.

php debugbar

 

This is why you should include Php Debugbar into Laravel Projetcs:

  • Easy to integrate with any project
  • Clean, fast and easy to use interface
  • Handles AJAX request
  • The client side bar is 100% coded in javascript
  • Very well Documented ( See the Documentation )

Installation

First of all we should add "barryvdh/laravel-debugbar": "1.*" to your composer.json in root folder. After that update the composer and add the ServiceProvider to the providers array in app/config/app.php'Barryvdh\Debugbar\ServiceProvider', . Then you need to publish the assets from this package php artisan debugbar:publish  If you want to use the facade to log messages, add this to your facades in app.php:  ‘Debugbar’ => ‘Barryvdh\Debugbar\Facade’,

Artisan commands Laravel Framework

Artisan is the name of the command-line interface included with Laravel. It provides a number of helpful commands for your use while developing your application. It is driven by the powerful Symfony Console component.

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

// Displays help for a given command
php artisan –help OR -h
// Do not output any message
php artisan –quiet OR -q
// Display this application version
php artisan –version OR -V
// Do not ask any interactive question
php artisan –no-interaction OR -n
// Force ANSI output
php artisan –ansi
// Disable ANSI output
php artisan –no-ansi
// The environment the command should run under
php artisan –env
// -v|vv|vvv Increase the verbosity of messages: 1 for normal output,
// 2 for more verbose output and 3 for debug
php artisan –verbose
[/cc]
[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]
// Display the framework change list
php artisan changes
// Remove the compiled class file
php artisan clear-compiled
// Put the application into maintenance mode
php artisan down
// Regenerate framework autoload files
php artisan dump-autoload
// Display the current framework environment
php artisan env
// Displays help for a command
php artisan help
// Lists commands
php artisan list
// Optimize the framework for better performance
php artisan optimize
// List all registered routes
php artisan routes
// Serve the application on the PHP development server
php artisan serve
// Interact with your application
php artisan tinker
// Bring the application out of maintenance mode
php artisan up

[/cc]

artisan
[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

// Create a new package workbench
php artisan workbench
// Publish a package’s assets to the public directory
php artisan asset:publish [–bench[=”vendor/package”]]
[–path[=”…”]] [package]
// Create a migration for the password reminders table
php artisan auth:reminders-table
// Flush the application cache
php artisan cache:clear
// Create a new Artisan command (L3:task)
php artisan command:make name [–command[=”…”]]
[–path[=”…”]] [–namespace[=”…”]]
// Publish a package’s configuration to the application
php artisan config:publish
// Create a new resourceful controller
php artisan controller:make [–bench=”vendor/package”]
// Seed the database with records
php artisan db:seed [–class[=”…”]] [–database[=”…”]]
// Set the application key
php artisan key:generate
[/cc]

Laravel Documentation for Artisan

Laravel 4.2 Release Notes

 

The full change list for this release by running the php artisan changes command from a 4.2 installation, or by viewing the change file on Github. These notes only cover the major enhancements and changes for the release.

Note: During the 4.2 release cycle, many small bug fixes and enhancements were incorporated into the various Laravel 4.1 point releases. So, be sure to check the change list for Laravel 4.1 as well!

PHP 5.4 Requirement

Laravel 4.2 requires PHP 5.4 or greater. This upgraded PHP requirement allows us to use new PHP features such as traits to provide more expressive interfaces for tools like Laravel Cashier. PHP 5.4 also brings significant speed and performance improvements over PHP 5.3.

Laravel Forge

Laravel Forge, a new web based application, provides a simple way to create and manage PHP servers on the cloud of your choice, including Linode, DigitalOcean, Rackspace, and Amazon EC2. Supporting automated Nginx configuration, SSH key access, Cron job automation, server monitoring via NewRelic & Papertrail, “Push To Deploy”, Laravel queue worker configuration, and more, Forge provides the simplest and most affordable way to launch all of your Laravel applications.

The default Laravel 4.2 installation’s app/config/database.php configuration file is now configured for Forge usage by default, allowing for more convenient deployment of fresh applications onto the platform.

More information about Laravel Forge can be found on the official Forge website.

laravel 4

Laravel Homestead

Laravel Homestead is an official Vagrant environment for developing robust Laravel and PHP applications. The vast majority of the boxes’ provisioning needs are handled before the box is packaged for distribution, allowing the box to boot extremely quickly. Homestead includes Nginx 1.6, PHP 5.6, MySQL, Postgres, Redis, Memcached, Beanstalk, Node, Gulp, Grunt, & Bower. Homestead includes a simple Homestead.yaml configuration file for managing multiple Laravel applications on a single box.

The default Laravel 4.2 installation now includes an app/config/local/database.php configuration file that is configured to use the Homestead database out of the box, making Laravel initial installation and configuration more convenient.

The official documentation has also been updated to include Homestead documentation.

Laravel Cashier

Laravel Cashier is a simple, expressive library for managing subscription billing with Stripe. With the introduction of Laravel 4.2, we are including Cashier documentation along with the main Laravel documentation, though installation of the component itself is still optional. This release of Cashier brings numerous bug fixes, multi-currency support, and compatibility with the latest Stripe API.

Daemon Queue Workers

The Artisan queue:work command now supports a --daemon option to start a worker in “daemon mode”, meaning the worker will continue to process jobs without ever re-booting the framework. This results in a significant reduction in CPU usage at the cost of a slightly more complex application deployment process.

More information about daemon queue workers can be found in the queue documentation.

Mail API Drivers

Laravel 4.2 introduces new Mailgun and Mandrill API drivers for the Mail functions. For many applications, this provides a faster and more reliable method of sending e-mails than the SMTP options. The new drivers utilize the Guzzle 4 HTTP library.

Soft Deleting Traits

A much cleaner architecture for “soft deletes” and other “global scopes” has been introduced via PHP 5.4 traits. This new architecture allows for the easier construction of similar global traits, and a cleaner separation of concerns within the framework itself.

More information on the new SoftDeletingTrait may be found in the Eloquent documentation.

Convenient Auth & Remindable Traits

The default Laravel 4.2 installation now uses simple traits for including the needed properties for the authentication and password reminder user interfaces. This provides a much cleaner default User model file out of the box.

“Simple Paginate”

A new simplePaginate method was added to the query and Eloquent builder which allows for more efficient queries when using simple “Next” and “Previous” links in your pagination view.

Migration Confirmation

In production, destructive migration operations will now ask for confirmation. Commands may be forced to run without any prompts using the --force command.

Get url parameter in Laravel

Let’s say we are working on page called planets. Somewhere in the page we want to use a parameter which we previously assigned in url. This url parameter would be the name of the planet, planets/venus.

How can me we get that planet name in Laravel? It’s pretty much simple.
First we have open routes.php under app. Then we should copy the following lines of code into routes.php

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

Route::get(‘planets/{theSubject}’, function($theSubject)
{
return ‘We are looking for‘.$theSubject.’‘;
});

[/cc]

Now if you open localhost/laravel/public/planets/venus it shows:

We are looking for venus

Google Trends Frameworks, Laravel approaching Zend

According to Google Trends Frameworks, on August 2014 there are four main PHP Frameworks: Zend, Symfony, Yii and Laravel. The most important goal of all these frameworks is replacing outdated procedures with clean and well documented code.

intolaravel.com-php-frameworks-google -trends-

 

Code Igniter, Yii and Symfony have been on the top of the classification since the beginning and they still are providing reliable, tested performance that many websites rely on.
Laravel is on the top of the charts, approaching Zend, which had its 100 points on year 2009.

– Here is why Laravel framework is becoming so popular.

 

Create a simple table with Laravel

In this tutorial we are going to create a table named ‘shops‘ in our database.

create table

For the next step we need to open command line. Change the directory of project by typing:

cd \xampp\htdocs\laravel

Using Artisan we are going to create the file that has our table features.

php artisan migrate:make create_shops_directory

This command with create a file 2014_07_29_071044_create_shops_table.php. Open this and paste the following:

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]
increments(‘id’);
$table->string(‘shop_name’);
$table->string(‘owner’);
$table->string(‘address’);
$table->boolean(‘active’)->default(1);
$table->text(‘description’);
$table->timestamps();
});
}

public function down(){

Schema::drop(‘shops’);
}

}
[/cc]

Your command line with show you a message like this:

Migrated: 2014_07_29_071044_create_shops_table.php

Now you simply type:

php artisan migrate

That’s it. Now you can open your PhpMyadmin to see the table ‘shops’ under ‘database’

Simple Laravel 4 routing tutorial

Routing on Laravel – Laravel Framework offers you a simple, easy and clean way for routing your pages. Routes are declared using the Route class. To create or modify a route you can go on app/routes.php.

Let’s say our domain is called localhost. Under that we need to create a page that talks about skateboards and it will have this url: http://localhost/skateboards. We will use the http protocol (used by most web browsers) to access our Laravel application hosted on localhost.

1. First go to app/routes.php and paste the following code.

[cc lang=”php” tab_size=”5″ lines=”40″ noborder=”1″]

/* app/route.php */

Route::get(‘/skateboards’, function()
{
return ” This is Skateboards page”;
});

[/cc]