Laraveler


Memodifikasi Default Login Laravel 5.1

Artikel ini untuk Laravel 5.1 dan ada baiknya Anda membaca dulu artikel tentang Autentikasi Default Laravel 5.1.

Jika Anda merasa fitur autentikasi bawaan Laravel tidak mencukupi kebutuhan dan berniat membuat autentikasi sendiri, sebaiknya baca dulu artikel ini sampai tuntas. Siapa tahu kebutuhan Anda sebenarnya bisa diakomodir, hanya saja belum tahu caranya.

1. Login Dengan Username

Default login Laravel menggunakan email dan password untuk mengidentifikasi akun. Namun kita bisa dengan mudah mengganti email menjadi username. Caranya seperti di bawah ini:

// 1. tambahkan property $username ke AuthController
class AuthController extends Controller
{
    protected $username = 'username'; // 'username' bisa diganti sesuai kebutuhan
    ...

// 2. ganti field email menjadi username di auth/login.blade.php
// sebelum
<input type="email" name="email" value="">

// sesudah
<input type="username" name="username" value="">

Sekali lagi, jika Anda belum pernah menggunakan autentikasi bawaan Laravel, baca kembali tutorial Autentikasi Default Laravel 5.1.

2. Mengganti Redirect Setelah Registrasi

Defaultnya setelah berhasil registrasi akan diredirect ke /home. Untuk mengubah url redirect, tambahkan property $redirectPath ke AuthController dan isi sesuai kebutuhan:

class AuthController extends Controller
{
 $redirectPath = 'dashboard/profile';

3. Mengganti Redirect Setelah Login

Jika halaman yang dituju setelah login sama dengan halaman yang dituju setelah registrasi, maka kita tidak perlu melakukan apa-apa. Tapi jika berbeda, misalnya setelah login diredirect ke dashboard/welcome, maka kita harus tambahkan fungsi berikut ini ke AuthController:

public function authenticated($request, $user)
{
 // Fungsi ini akan dipanggil setelah user berhasil login.
 // Kita bisa menambahkan aksi-aksi lainnya, misalnya mencatat waktu last_login user.
 return redirect('dashboard/welcome');
}

4. Menambahkan Pengecekan Status User

Seringkali tabel user memiliki kolom status yang isinya active, blocked atau pending. Lalu hanya user yang statusnya active yang boleh login. Login default Laravel tidak melakukan pengecekan seperti ini. Untuk itu kita harus memodifikasi lagi AuthController dengan menambahkan fungsi seperti ini:

// Fungsi getCredentials ini aslinya ada di Illuminate\Foundation\Auth\AuthenticatesUsers.
// Yang dilakukan disini adalah meng-override fungsi tersebut dan menambahkan kolom status dalam pengecekan.
protected function getCredentials(Request $request)
{
 // aslinya
 // return $request->only($this->loginUsername(), 'password');
  
 // dimodifikasi jadi seperti ini
 return $request->only($this->loginUsername(), 'password') + ['status' => 'active'];
}

5. Otomatis Logout Jika Browser Ditutup

Buka file config/sessions.php, cari baris berikut dan ubah nilainya menjadi true:

'expire_on_close' => false,

Penutup

Sekian 5 modifikasi autentikasi bawaan Laravel versi On The Spot. Trik-trik seperti ini tidak ditemui dalam dokumentasi resmi di website laravel.com. Hanya Tuhan, Taylor Otwell, dan Anda yang suka menelusuri source code yang tahu. Keep reading !

Read more »

Cara Intall Laravel 5.6 2018

Saya rekomendasikan Anda untuk menginstall composer terlebih dahulu. Atau Anda bisa membaca kembali postingan terdahulu tentang cara install composer di website ini.

Cara 1: Via Installer Laravel

Pertama, download installer laravel:

composer global require "laravel/installer"

Selanjutnya, jalankan perintah berikut:

laravel new blog

Kelebihan

  • Relatif lebih cepat.
  • Keywordnya lebih simpel dan mudah diingat.
  • Dijamin mendapatkan update kode terbaru.

Kekurangan

  • Perlu koneksi internet untuk mendowloand library lainnya.
  • Perlu satu langkah tambahan untuk mendownload installer laravel.

Perkiraan Waktu

  • 2 menit*

Cara 2: Via composer create-project

Setelah Anda berhasil menginstall composer, cukup jalankan perintah berikut ini di terminal/console/command prompt Anda:

composer create-project --prefer-dist laravel/laravel blog

Kelebihan

  • Singkat, cukup satu langkah.

Kekurangan

  • Perlu koneksi internet untuk mendowloand library lainnya.
  • Relatif membutuhkan waktu yang lama.

Perkiraan Waktu

  • 10 menit*

Cara 3: Download Source Laravel Secara Lengkap

Klik disini untuk mendownload source Laravel siap pakai (sudah lengkap dengan library lain yang dibutuhkan). Ekstrak file tersebut di document root server Anda (htdocs, www, atau yang sejenisnya). Laravel siap dijalankan.

Kelebihan

  • Ini cara yang paling dikenal oleh rata-rata programmer: download source code, taruh di htdocs, aplikasi bisa langsung diakses.
  • Relatif paling cepat.
  • Tidak perlu koneksi internet untuk mendownload library lain yang dibutuhkan.

Kekurangan

  • Bisa jadi kode yang Anda download bukan kode terbaru. Oleh karena itu cara ini sebenarnya tidak dianjurkan, kecuali Anda cuma ingin mencicipi Laravel secara cepat dan tidak mau dipusingkan dengan composer.

Perkiraan Waktu

  • 1 menit*

Untuk cara ketiga, sebenarnya update library tetap bisa dilakukan kapanpun Anda mau. Begitu terhubung dengan internet, cukup jalankan perintah composer update maka otomatis library akan diperbarui. Yang membedakan hanyalah cara Anda mendapatkan library-library tersebut untuk pertama kali.

*Perkiraan waktu didapatkan dengan ujicoba pada jaringan internet dengan kecepatan download 200 KB/s.

Read more »

Configuration Laravel 5.6 2018

Configuration

Introduction

All of the configuration files for the Laravel framework are stored in the config directory. Each option is documented, so feel free to look through the files and get familiar with the options available to you.

Environment Configuration

It is often helpful to have different configuration values based on the environment where the application is running. For example, you may wish to use a different cache driver locally than you do on your production server.

To make this a cinch, Laravel utilizes the DotEnv PHP library by Vance Lucas. In a fresh Laravel installation, the root directory of your application will contain a .env.example file. If you install Laravel via Composer, this file will automatically be renamed to .env. Otherwise, you should rename the file manually.

Your .env file should not be committed to your application's source control, since each developer / server using your application could require a different environment configuration. Furthermore, this would be a security risk in the event an intruder gain access to your source control repository, since any sensitive credentials would get exposed.

If you are developing with a team, you may wish to continue including a .env.example file with your application. By putting place-holder values in the example configuration file, other developers on your team can clearly see which environment variables are needed to run your application. You may also create a .env.testing file. This file will override the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option.

{tip} Any variable in your .env file can be overridden by external environment variables such as server-level or system-level environment variables.

Environment Variable Types

All variables in your .env files are parsed as strings, so some reserved values have been created to allow you to return a wider range of types from the env() function:

.env Value env() Value
true (bool) true
(true) (bool) true
false (bool) false
(false) (bool) false
empty (string) ''
(empty) (string) ''
null (null) null
(null) (null) null

If you need to define an environment variable with a value that contains spaces, you may do so by enclosing the value in double quotes.

APP_NAME="My Application"

Retrieving Environment Configuration

All of the variables listed in this file will be loaded into the $_ENV PHP super-global when your application receives a request. However, you may use the env helper to retrieve values from these variables in your configuration files. In fact, if you review the Laravel configuration files, you will notice several of the options already using this helper:

'debug' => env('APP_DEBUG', false),

The second value passed to the env function is the "default value". This value will be used if no environment variable exists for the given key.

Determining The Current Environment

The current application environment is determined via the APP_ENV variable from your .env file. You may access this value via the environment method on the App facade:

$environment = App::environment();

You may also pass arguments to the environment method to check if the environment matches a given value. The method will return true if the environment matches any of the given values:

if (App::environment('local')) {
    // The environment is local
}

if (App::environment(['local', 'staging'])) {
    // The environment is either local OR staging...
}

{tip} The current application environment detection can be overridden by a server-level APP_ENV environment variable. This can be useful when you need to share the same application for different environment configurations, so you can set up a given host to match a given environment in your server's configurations.

Accessing Configuration Values

You may easily access your configuration values using the global config helper function from anywhere in your application. The configuration values may be accessed using "dot" syntax, which includes the name of the file and option you wish to access. A default value may also be specified and will be returned if the configuration option does not exist:

$value = config('app.timezone');

To set configuration values at runtime, pass an array to the config helper:

config(['app.timezone' => 'America/Chicago']);

Configuration Caching

To give your application a speed boost, you should cache all of your configuration files into a single file using the config:cache Artisan command. This will combine all of the configuration options for your application into a single file which will be loaded quickly by the framework.

You should typically run the php artisan config:cache command as part of your production deployment routine. The command should not be run during local development as configuration options will frequently need to be changed during the course of your application's development.

{note} If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

Maintenance Mode

When your application is in maintenance mode, a custom view will be displayed for all requests into your application. This makes it easy to "disable" your application while it is updating or when you are performing maintenance. A maintenance mode check is included in the default middleware stack for your application. If the application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503.

To enable maintenance mode, execute the down Artisan command:

php artisan down

You may also provide message and retry options to the down command. The message value may be used to display or log a custom message, while the retry value will be set as the Retry-After HTTP header's value:

php artisan down --message="Upgrading Database" --retry=60

Even while in maintenance mode, specific IP addresses or networks may be allowed to access the application using the command's allow option:

php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16

To disable maintenance mode, use the up command:

php artisan up

{tip} You may customize the default maintenance mode template by defining your own template at resources/views/errors/503.blade.php.

Maintenance Mode & Queues

While your application is in maintenance mode, no queued jobs will be handled. The jobs will continue to be handled as normal once the application is out of maintenance mode.

Alternatives To Maintenance Mode

Since maintenance mode requires your application to have several seconds of downtime, consider alternatives like Envoyer to accomplish zero-downtime deployment with Laravel.

Read more »


© adimancv.com