New Helper functions In Laravel 5

Hi guys, I have compiled a list of new helper functions that will be available in Laravel 5 in addition to the current ones.

Routing

get($uri,$action)

Registers a route to handle HTTP GET requests just like Route::get method call but with less code.

Examples:

//usage with closure.
get('/',function(){
    return 'home page';
});

//usage with array as action argument.
get('/',['as'=>'home','uses'=>'HomeController@index']);

post($uri,$action)

Registers a route to handle HTTP POST requests just like Route::post method call but with less code.

Examples:

//usage with closure.
post('/login',function(){
    //handle post login here.
});

//usage with array as action argument.
post('/login',['as'=>'auth.login','uses'=>'AuthController@postLogin']);

delete($uri,$action)

Registers a route to handle HTTP DELETE requests just like Route::delete method call but with less code.

Examples:

//usage with closure
delete('/post/{id}',function($id){
    //destroy logic here.
});

//usage with array as action argument.
delete('/post/{id}',['as'=>'post.destroy','uses'=>'PostController@destroy']);

put($uri,$action)

Registers a route to handle HTTP PUT requests just like Route::put method call but with less code.

Examples:

//usage with closure
put('/post/{id}',function($id){
    //update logic here.
});

//usage with array as action argument.
put('/post/{id}',['as'=>'post.update','uses'=>'PostController@update']);

View and Redirect

view($view,$data,$mergeData)

Renders a view passed as $view argument. It is same as calling the View::make method.

Examples:

return view('home'); //renders the home page.

//calling with data.

return view('home',['title','Awesome']);

//usage with master layout

return view('master',['content'=>view('index',compact('data'))]);

redirect($to)

Generates a redirect response for the specified uri as the $to argument.

Example:

return redirect('/home');

Hashing

bcrypt($value,$options)

Generates the hash for the given value. You can use the bcrypt helper function to generate the password hashes.

Example:

$user->password = bcrypt($request->password);

Logging

info($message,$context)

Writes an information message to the log file, and serves as a shorthand for Log::info.

Example:

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

Configurations

config($key,$default)

config helper function returns the value associated with the $key passed as the argument.

Example:

echo config('app.debug');

//we can pass a default value as a second argument.

echo config('app.url','default');

I hope you will find this list useful, Thanks!

One thought on “New Helper functions In Laravel 5”

  1. Also worth mentioning, redirect() returns app(‘redirect’) if no $to is specified so redirect()->back(); works in place of Redirect::back()

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.