Wednesday, May 27, 2015

Contoh penggunaan facade route::

1. GET Route - terima URI:

Route::get('/', function()
{
    return 'Hello World';
});

2. Routes POST, PUT, DELTE :

Route::post('foo/bar', function()
{
    return 'Hello World';
});

Route::put('foo/bar', function()
{
    //
});

Route::delete('foo/bar', function()
{
    //
});

3. Route untuk verb berlainan :

Route::match(['get', 'post'], '/', function()
{
    return 'Hello World';
});

4. Route semua HTTP Verb :

Route::any('foo', function()
{
    return 'Hello World';
});



nota : 

- url helper:

$url = url('foo');

- masukkan token :
 
<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
 
atau untuk blade :
 
<input type="hidden" name="_token" value="{{ csrf_token() }}">

 5. addtional method

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
</form>

6. Route Parameter
 
Route::get('user/{id}', function($id)
{
    return 'User '.$id;
});


- dengan pilihan

Route::get('user/{name?}', function($name = null)  
{ 
   return $name; 
});
- dengan pilihan dan default

Route::get('user/{name?}', function($name = 'John')  
{ 
   return $name; 
});

7. Menggunakan Regular Expression :

Route::get('user/{name}', function($name)
{
    //
})
->where('name', '[A-Za-z]+');

Route::get('user/{id}', function($id)
{
    //
})
->where('id', '[0-9]+');

 

8. Kekangan array :

Route::get('user/{id}/{name}', function($id, $name)
{
    //
})
->where(['id' => '[0-9]+', 'name' => '[a-z]+']);

 

8. Definisi Global Patterns

set dalam RouteServiceProvider:
 
$router->pattern('id', '[0-9]+');
 
 
Route::get('user/{id}', function($id)
{
    // Only called if {id} is numeric.
});

 

9. semak parameter Route :

 
if ($route->input('id') == 1)
{
    //
}


atau

use Illuminate\Http\Request;

Route::get('user/{id}', function(Request $request, $id)  
{ 
   if ($request->route('id'))  
   { 
      // 
   } 
});


11. Namakan routes

Route::get('user/profile', ['as' => 'profile', function()
{
    //
}]);
 
 
You may also specify route names for controller actions:
 
Route::get('user/profile', [
    'as' => 'profile', 'uses' => 'UserController@showProfile'
]);

Now, you may use the route's name when generating URLs or redirects:
 
$url = route('profile');

$redirect = redirect()->route('profile');


The currentRouteName method returns the name of the route handling the current request:
$name = Route::currentRouteName();

Route Groups

Sometimes many of your routes will share common requirements such as URL segments, middleware, namespaces, etc. Instead of specifying each of these options on every route individually, you may use a route group to apply attributes to many routes.
Shared attributes are specified in an array format as the first parameter to the Route::group method.

Middleware

Middleware are applied to all routes within the group by defining the list of middleware with the middleware parameter on the group attribute array. Middleware will be executed in the order you define this array:
Route::group(['middleware' => ['foo', 'bar']], function()
{
    Route::get('/', function()
    {
        // Has Foo And Bar Middleware
    });

    Route::get('user/profile', function()
    {
        // Has Foo And Bar Middleware
    });

});

Namespaces

You may use the namespace parameter in your group attribute array to specify the namespace for all controllers within the group:
Route::group(['namespace' => 'Admin'], function()
{
    // Controllers Within The "App\Http\Controllers\Admin" Namespace

    Route::group(['namespace' => 'User'], function()
    {
        // Controllers Within The "App\Http\Controllers\Admin\User" Namespace
    });
});
Note: By default, the RouteServiceProvider includes your routes.php file within a namespace group, allowing you to register controller routes without specifying the full App\Http\Controllers namespace prefix.

Sub-Domain Routing

Laravel routes also handle wildcard sub-domains, and will pass your wildcard parameters from the domain:

Registering Sub-Domain Routes

Route::group(['domain' => '{account}.myapp.com'], function()
{

    Route::get('user/{id}', function($account, $id)
    {
        //
    });

});

Route Prefixing

A group of routes may be prefixed by using the prefix option in the attributes array of a group:
Route::group(['prefix' => 'admin'], function()
{
    Route::get('users', function()
    {
        // Matches The "/admin/users" URL
    });
});
You can also utilize the prefix parameter to pass common parameters to your routes:

Registering a URL parameter in a route prefix

Route::group(['prefix' => 'accounts/{account_id}'], function()
{
    Route::get('detail', function($account_id)
    {
        //
    });
});
You can even define parameter constraints for the named parameters in your prefix:
Route::group([
    'prefix' => 'accounts/{account_id}',
    'where' => ['account_id' => '[0-9]+'],
], function() {

    // Define Routes Here
});

Route Model Binding

Laravel model binding provides a convenient way to inject class instances into your routes. For example, instead of injecting a user's ID, you can inject the entire User class instance that matches the given ID.
First, use the router's model method to specify the class for a given parameter. You should define your model bindings in the RouteServiceProvider::boot method:

Binding A Parameter To A Model

public function boot(Router $router)
{
    parent::boot($router);

    $router->model('user', 'App\User');
}
Next, define a route that contains a {user} parameter:
Route::get('profile/{user}', function(App\User $user)
{
    //
});
Since we have bound the {user} parameter to the App\User model, a User instance will be injected into the route. So, for example, a request to profile/1 will inject the User instance which has an ID of 1.
Note: If a matching model instance is not found in the database, a 404 error will be thrown.
If you wish to specify your own "not found" behavior, pass a Closure as the third argument to the model method:
Route::model('user', 'User', function()
{
    throw new NotFoundHttpException;
});
If you wish to use your own resolution logic, you should use the Route::bind method. The Closure you pass to the bind method will receive the value of the URI segment, and should return an instance of the class you want to be injected into the route:
Route::bind('user', function($value)
{
    return User::where('name', $value)->first();
});

Throwing 404 Errors

There are two ways to manually trigger a 404 error from a route. First, you may use the abort helper:

abort(404);



 
 




 

Tuesday, May 26, 2015

Contoh penggunaan facade session::

1. Simpan item dalam session :

Session::put('key', 'value'); 
 
method :
 
 session(['key' => 'value']);

2. masukkan nilan dalam item session :

Session::push('user.teams', 'developers');

3. Dapatkan item dalam session :

$value = Session::get('key'); 

method :
 
$value = session('key');

4. Dapatkan item dari session :

$value = Session::get('key', 'default'); 

atau 
 
$value = Session::get('key', function() { return 'default'; });

5. Dapatkan Item dan lupakan :

$value = Session::pull('key', 'default');

6. Datapakan semua data dari session :

$data = Session::all();

7. Pastikan item wujud dalam session :

if (Session::has('users')) { // }

8. Buang item dalam session : 

Session::forget('key');

9. Buang semua item dalam session :

Session::flush();

10. Bina id session baru :

Session::regenerate();

 


flash data : simpan data untuk request yang seterusnya.


11. Reflash data semasa untuk request seterusnya :

Session::reflash();

12. Reflash habya sebahagian Flash Data

Session::keep(['username', 'email']);



Guna database sebagai session data :

Contoh  Schema  table:
 
Schema::create('sessions', function($table)
{
    $table->string('id')->unique();
    $table->text('payload');
    $table->integer('last_activity');
});

atau
 
php artisan session:table

composer dump-autoload

php artisan migrate

Monday, May 25, 2015

Contoh penggunaan facade html::

1. Tukar HTML String kepada Entities, php htmlentities() :

echo HTML::entities('<h1>Title example</h1>');
 
nota : tikar '<' ke &lt; dan '>' ke &gt; 
 
guna helper :
 
echo e('<h1>Title example</h1>'); 




2. Tukar HTML Entities ke String, php html_entity_decode() :

$string HTML::decode('&lt;h1&gt;Hello&lt;/h1&gt;');
 
 

3. Bina pautan ke fail Javascript :

{{ HTML::script('js/functions.js') }}
 
hasil :
 
<script src="http://your.url/js/functions.js"></script>
 
- atribute tambahan :
 
{{ HTML::script('js/functions.js', array('async' => 'async')) }}  
 
hasil :
 
<script async="async" src="http://your.url/js/functions.js"></script>
 
 
4. Bina pautan ke fail CSS

{{ HTML::style('css/style.css') }}
 
hasil :
 
<link media="all" type="text/css" rel="stylesheet"
  href="http://your.url/css/style.css">
 
-dengan atribute tambahan
 
{{ HTML::style('css/style.css', array('media' => 'print')) }}
 
hasil :
 
<link media="print" type="text/css" rel="stylesheet"
  href="http://your.url/css/style.css">
 
    


5. Bina elemen imej untuk HTML :

{{ HTML::image('img/picture.jpg') }}
 
hasil :
 
<img src="http://your.url/img/picture.jpg">
 
-tambah alt :
 
{{ HTML::image('img/picture.jpg', 'a picture') }}
 
hasil :
 
<img src="http://your.url/img/picture.jpg" alt="a picture">
 
- dengan atribute tambahan :
 
<img src="http://your.url/img/picture.jpg" class="thumb" alt="a picture">
 
     



6. Bina pautan untuk HTML :

{{ HTML::link('http://test.com') }}
 
hasil :
 
<a href="http://test.com">http://test.com</a>
 
 
- tambah title :
 
{{ HTML::link('http://test.com', 'testing')}}
 
hasil :
 
<a href="http://test.com">testing</a>
 
- tambah argument lain :
 
{{ HTML::link('http://test.com', null, array('id' => 'linkid'))}}
 
hasil :
 
<a href="http://test.com" id="linkid">http://test.com</a>
 
- tambah secure option :
 
{{ HTML::link('/login', 'log in', array('id' => 'linkid'), true)}}
 
hasil :
 
<a href="https://your.url/login" id="linkid">log in</a>       



7. Bina pautan Secure HTML :

{{ HTML::secureLink('x') }}
 
hasil :
 
<a href="https://your.url/x">https://your.url/x</a>
 
- tambah title :
 
{{ HTML::secureLink('a/b', 'A-B') }}
 
hasil :
 
<a href="https://url.url/a/b">A-B</a>
 
- tambah lain atribute :
 
{{ HTML::secureLink('login', 'Sign In', array('class' => 'btn')) }}
 
hasil :
 
<a href="https://your.url/login" class="btn">Sign In</a>   
   



8. Bina pautan asset HTML :

 HTML::linkAsset()

nota : bergantung kepada kedudukan index.php



9. Bina pautan asset  Secure HTML :

 HTML::linkSecureAsset()


10. Bina pautan HTML ke nama Route :

{{ HTML::linkRoute('login') }}
 
hasil :
 
<a href="http://your.url/user/login">http://your.url/user/login</a>
 
- dengan atribute :
 
{{ HTML::linkRoute('login', 'Sign In') }}
 
hasil :
 
<a href="http://your.url/user/login">Sign In</a>
 
- dengan parameter :
 
{{ HTML::linkRoute('items.show', 'Show item #4', array(4)) }}
 
hasil :
 
<a href="http://your.url/items/4">Show item #4</a>    
 
- tambahan parameter :
 
{{ HTML::linkRoute('login', 'Sign In', array(), array('class' => 'btn')) }}
 
hasil :
 
<a href="http://your.url/user/login" class="btn">Sign In</a>
   
  
11. Bina pautan HTML ke  Controller Action :

{{ HTML::linkAction('Home@index') }}
 
hasil :
 
<a href="http://your.url/index">http://your.url/index</a>
 
- dengan argument :
 
{{ HTML::linkAction('Home@index', 'Home') }}  
 
hasil :
 
<a href="http://your.url/index">Home</a>
 
-degan parameter :
 
{{ HTML::linkAction('ItemController@show', 'Show Item #3', array(3)) }}
 
hasil :
 
<a href="http://your.url/items/3">Show Item #3</a>
 
- dengan atribute tambahan :
 
{{ HTML::linkAction('Home@index', 'Home', array(), array('class' => 'btn')) }}
 
hasil :
 
<a href="http://your.url/index" class="btn">Home</a>
 
     
12. Bina pautan  HTML ke alamat Email :

{{ HTML::mailto('a@b.c') }}
 
hasil :
 
<a href="ma&#105;&#x6c;&#116;o&#58;&#97;&#64; \
  &#x62;.&#99;">&#97;&#64;&#x62;.&#99;</a>
 
- dengan 
 
{{ HTML::mailto('a@b.c', 'Email Me') }}
 
hasil   
 
<a href="m&#x61;i&#108;&#x74;&#x6f;&#x3a;&#x61; \
  &#x40;&#98;&#46;&#x63;">Email Me</a> 

- tambahan atribute :
{{ HTML::mailto('a@b.c', 'Email Me', array('class' => 'btn')) }}
 
hasil :
 
<a href="&#109;&#97;&#105;&#108;&#x74;o&#x3a; \
  &#97;&#64;b.&#x63;" class="btn">Email Me</a>
  

13. Obfuscating alamat Email :
-  tapisan untuk spam-bots.

$email = HTML::email('me@local.com');
 
hasil :
 
Email is <b>&#x6d;e&#x40;l&#111;ca&#x6c;.c&#x6f;m</b>
 
  



14. Bina item untuk Ordered List :

 
{{ HTML::ol(array('a', 'b', 'c')) }}
 
hasil :
 
<ol>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ol>
 
 
 
contoh sublist :
 
// PHP code to generate $list
$list = array(
  'one',
  'two',
  array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));
 
{{ HTML::ol($list) }}
 
hasil :
 
<ol>
  <li>one</li>
  <li>two</li>
  <li><ol>
    <li>sub-one</li>
    <li>sub-two</li>
  </ol></li>
</ol>
 
 
contoh 2 - sublist with title
 
// PHP code to generate $list
$list = array(
  'one',
  'two',
  'three' => array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));
 
{{ HTML::ol($list) }}
 
hasil :
 
<ol>
  <li>one</li>
  <li>two</li>
  <li>three
    <ol>
      <li>sub-one</li>
      <li>sub-two</li>
    </ol>
  </li>
</ol>
 
 
dengan atribute :
 
{{ HTML::ol(array('a', 'b'), array('class' => 'mylist')) }}
 
hasil :
 
<ol class="mylist">
  <li>a</li>
  <li>b</li>
</ol>
 
          
 

15. Bina item untuk  Unordered List :

{{ HTML::ul(array('a', 'b', 'c'))}}
 
hasil :
 
<ul>
  <li>a</li>
  <li>b</li>
  <li>c</li>
</ul>
 
-dengan sub list :
 
// PHP code to generate $list
$list = array(
  'one',
  'two',
  array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));
 
{{ HTML::ul($list) }}
 
hasil :
 
 
<ul>
  <li>one</li>
  <li>two</li>
  <li>
    <ul>
      <li>sub-one</li>
      <li>sub-two</li>
    </ul>
  </li>
</ul>
 
 
- sublist dengan title :
 
// PHP code to generate $list
$list = array(
  'one',
  'two',
  'three' => array(
    'sub-one',
    'sub-two',
  ),
);
return View::make('thebladeview', array('list' => $list));
 
{{ HTML::ul($list) }}
 
<ul>
  <li>one</li>
  <li>two</li>
  <li>three
    <ul>
      <li>sub-one</li>
      <li>sub-two</li>
    </ul>
  </li>
</ul>
 
- dengan atribute 
 
{{ HTML::ul(array('a', 'b'), array('class' => 'mylist')) }}
 
hasil :
    
<ul class="mylist">
  <li>a</li>
  <li>b</li>
</ul> 
      




15. Bina  HTML Attribute String dari Array :

echo HTML::attributes(array('id' => '123', 'class' => 'myclass'));
 
hasil :
 
id="123" class="myclass" 




16. Obfuscating  String :

{{-- Blade template --}}
{{ HTML::obfuscate('me@gmail.com') }}
 
&#x6d;&#x65;&#x40;&#x67;&#x6d;&#97;i&#x6c;. \
  &#99;&#x6f;&#x6d;