/RestAPILaravel

REST API Laravel 5.4 with Token Authentication

Primary LanguagePHP

REST API Laravel 5.4 with Token Authentication

Projeyi direkt kullanabilmek için:
$ git clone https://github.com/dilekuzulmez/RestAPILaravel.git 
$ cd Laravel-Realtime-Chat
$ composer install 
$ php artisan key:generate
$ php artisan serve 
ADIM 1:
$ composer create-project laravel/laravel RestAPILaravel 
$ cd RestAPILaravel
ADIM 1.1:
$ mysql -u [username] -p
mysql> create database api;
mysql> exit 
ADIM 1.2:

Projemizin .env dosyasına giderek veritabanı bilgilerimizi dolduruyoruz.

DB_CONNECTION=mysql
DB_HOST=replace with your IP
DB_PORT=3306
DB_DATABASE=api
DB_USERNAME=[username]
DB_PASSWORD=[password] 
ADIM 2:

Users migration tablomuza giderek aşağıdaki şekilde dosyayı güncelliyoruz. [2014_10_12_000000_create_users_table.php]

public function up()
{
  Schema::create('users', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->string('email')->unique();
  $table->string('password');
  $table->string('api_token', 60)->unique();
  $table->rememberToken();
  $table->timestamps();
  });
}

User modelimize giderek aşağıdaki şekilde güncelliyoruz.[User.php]

protected $fillable = [
        'name', 'email', 'password', **'api_token'**,
];
ADIM 2.1:

Şimdi Post modelimizi ve migrations'ımızı oluşturmak için aşağıdaki komutu çalıştırıyoruz.

$  php artisan make:model Post -m 

Post modelimize giderek aşağıdaki şekilde Foreign key olarak User modeli ile olan ilişkisini yazıyoruz.[Post.php]

public function user() {
   return $this->belongsTo(User::class);
} 

User modelimize giderek aşağıdaki ilişkiyi yazıyoruz.

public function posts() {
   return $this->hasMany(Post::class);
} 

Posts migration tablomuza giderek aşağıdaki şekilde dosyayı güncelliyoruz.[2017_08_23_194621_create_posts_table.php]

public function up()
{
  Schema::create('posts', function (Blueprint $table) {
  $table->increments('id');
  $table->string('title');
  $table->text('body');
  $table->integer('user_id');
  $table->timestamps();
  });
}

Post modelimize giderek aşağıdaki şekilde güncelliyoruz.[Post.php]

protected $fillable = [
  'title', 'body', 'user_id',
];

Aşağıdaki komut ile veritabanını güncelliyoruz.

$ php artisan migrate:refresh 
ADIM 3:

Şimdi bir APIController oluşturacağız.

$ php artisan make:controller APIController --resource 

APIController'ımızı aşağıdaki şekilde güncelleyelim.[APIController.php]

public function index()
{
  $posts = Post::all();
  return response()->json($posts);
}

!!Dosyamızın en üst kısmına bunu yazmayı unutmuyoruz:

use App\Post; 
ADIM 4:
$ php artisan tinker 'komutunu çalıştırıyoruz.

User eklemek için:

Post eklemek için:

ADIM 5:

Router'ı düzenleceğiz şimdi. [api.php]

Route::group(['middleware' => 'auth:api'], function () {
    Route::resource('post', 'APIController');
}); 
ADIM 6:

Authentication'ı etkinleştiriyoruz.

$ php artisan make:auth 
ADIM 7:
$ php artisan serve

Şimdi browserımızı açıp URL'a api_token'ımızı yazacağız.

localhost:8000/api/post?api_token=uOunEWjLCl0qhhMYR6lI1j8qY3aYfbc3pQa48Bq1KOrNaH2NXjw12VHtdg3d 

Ve mutlu son 🎉 🎉 🎉