20% Discount for all products. Limited Availability.

How to enable CORS in Laravel 5

A resource makes a cross-origin HTTP request when it requests a resource from a domain or port which is different from the one which the first resource itself serves. For example, an HTML page served from http://domain-a.com makes an  img src request for http://domain-b.com/image.jpg. Many pages on the web today load resources like CSS stylesheets, images and scripts from separate domains.

For security reasons, browsers restrict cross-origin HTTP requests initiated from within scripts. For example, XMLHttpRequest and Fetch follow the same-origin policy. So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own domain. To improve web applications, developers asked browser vendors to allow cross-domain requests.

See more >>

This video tutorial is all about enabling CORS in Laravel.


1. Add middleware

php artisan make:middleware Cors
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');

2. Register middleware as a route middleware

3. Use middleware in routes which support CORS


Passing Multiple Parameters to Form Validation Custom Callbacks in Codeigniter

CI natively only allows for single parameter in custom callbacks. For an instance see the following code.

('hstate', 'State', 'required|callback_suburb_check[' . $suburb . ']');

If you need to pass multiple parameters, you have several options. Obviously you can change CI behaviour by subclassing the core library. But in this tutorial we follow the less pain approach. That ‘s to access required parameters via POST variables within your custom callback function. They are still available in this scope.

There is another way using your PHP string handling knowledge. Just formatting all the parameters as single string and passing to the callback function.

$parameters = 'first_arg' . '||' . 'second_arg' . '||' . 'third_arg';
('some_field', 'Some Field Name', 'callback__my_callback_function['.$parameters.']');

Then in your callback,

function _my_callback_function($field_value, $second_parameter){
    list($first_param, $second_param, $third_param) = split('||', $second_parameter);

iPhone Push Notifications PHP Server

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', YOUR_.PEM_FILE);
stream_context_set_option($ctx, 'ssl', 'passphrase', YOUR_PASSPHRASE);
$fp = stream_socket_client(
 'ssl://gateway.sandbox.push.apple.com:2195', $err, $errstr, 60, STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
 exit("Failed to connect: $err $errstr<BR><BR>");
echo 'Connected to APN<BR>';
// Create the payload body
$body['aps'] = array(
 'badge' => +1,
 'alert' => 'Testing push notifications',
 'sound' => 'new_wall_paper.wav',
 'action-loc-key' => 'OK'
$payload = json_encode($body);
for ($i = 0; $i < count($tokens); $i++) {
 $msg = chr(0) . pack('n', 32) . pack('H*', $tokens [$i]) . pack('n', strlen($payload)) . $payload;
 fwrite($fp, $msg, strlen($msg));
echo "Completed sending messages";