Kurulum

Php kütüphanelerinin kurulumu için composer paketi bilgisayarınızda kurulu olmalıdır.

Gereksinimler

Kurulumdan önce lütfen aşağıdaki bileşenleri çalışma ortamınıza yükleyin.

  • Composer
  • Php 8.0 veya üstü sürümler
  • Apache 2 veya üstü sürümler
  • Apache Mod Rewrite
  • Redis Server
  • Php Redis Extension

Olobase projesi arka uç uygulaması oluşturmak için önce github php depo sundan clone bağlantısını kopyalayın ve ardından aşağıdaki git clone komutuyla birlikte konsolunuza yapıştırın.

git clone --branch 1.3.1 [email protected]:olomadev/olobase-skeleton-php.git example-php

Composer paketlerini yükleyin.

/var/www/example-php$ composer install

/data/tmp önbellek klasörü için 777 iznini verin.

chmod 777 -R /var/www/example-php/data/tmp

Mezzio Hakkında

Olobase, arka uçta Mezzio Çerçevesini kullanır. Mezzio framework'ü hakkında daha detaylı bilgi almak isterseniz aşağıdaki kaynaklara göz atabilirsiniz.

Kaynak Url Açıklama
Dökümentasyon https://docs.mezzio.dev/ Mezzio resmi dökümentasyon web adresi
Kurulum https://matthewsetter.com/how-to-create-a-mezzio-application/ Mezzio'nun yaratıcısı tarafından yazılmış ve Mezzio kurulumu hakkında geniş bilgi içeren blog adresi

Apache2 Konfigürasyonu

Apache mod_rewrite eklentisini etkileştirin.

sudo a2enmod rewrite

apache2.conf dosyasında dizinler için AllowOverride seçeneği All değerinde olmalıdır.

vim /etc/apache2/apache2.conf

Varsayılan None olan bu seçeneği All olarak değiştirin.

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Bir host dosyası oluşturun

cd /etc/apache2/sites-available
cp 00-default.conf example.local.conf
vim example.local.conf

Apache host dosyanızda DocumentRoot konfigürasyonu projeniz/public/ klasörüne ayarlanmış olmalıdır. DirectoryIndex değerini index.php değerine ayarlamanız önerilir. ServerName bu örnekte example.local olarak ayarlanmıştır.

<VirtualHost *:80>

    SetEnv "APP_ENV" "local"
    ServerName example.local
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/olobase-skeleton-php/public/
    DirectoryIndex index.php

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

vhost dosyasınızı kaydedin ve apache sunucusunu yeniden başlatın.

a2ensite example.local.conf
sudo service apache2 restart

.htaccess

Konfigurasyon aşağıdaki gibi .htaccess dosyasını gerektirir. Bu dosya /public klasöründe mevcuttur.

# Disable directory indexing
# Options -Indexes
# Options +FollowSymLinks
# Options -MultiViews

php_value post_max_size 10M
php_value upload_max_filesize 10M

RewriteEngine On
#
# Redirect www to non-www
#
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# The following rule allows authentication to work with fast-cgi
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

# The following rewrites all other queries to index.php. The
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size
# fits all solution.

RewriteCond %{REQUEST_URI} !/api/
RewriteRule ^(.*)$ %{ENV:BASE}index.html [NC,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

Linux Host Dosyası

Bu örnekte /etc/hosts dosyasına proje example.local olarak tanımlanmıştır.

127.0.1.1       example.local

Bu tanımlamadan sonra tarayıcınızdan aşağıdaki gibi proje ismi ile uygulamanıza ulaşabilirsiniz.

http://example.local/

Yerel Ortam Konfigürasyonu

Yerel ortamda çalışmak için projeniz içinde /config/autoload/ klasöründeki local.php.dist dosyasını kopyalayıp local.php adı ile kaydedin.

<?php
declare(strict_types=1);

use Laminas\ConfigAggregator\ConfigAggregator;

return [
    // Toggle the configuration cache. Set this to boolean false, or remove the
    // directive, to disable configuration caching. Toggling development mode
    // will also disable it by default; clear the configuration cache using
    // `composer clear-config-cache`.
    ConfigAggregator::ENABLE_CACHE => false,

    // Enable debugging; typically used to provide debugging information within templates.
    'debug' => true,
    'token' => [
        // Cookie encryption
        'encryption' => [
            'iv' => '', // generate random 16 chars
            'enabled' => false, // it should be true in production environment
            'secret_key' => '',
        ],
        // Public and private keys are expected to be Base64 encoded.
        // The last non-empty line is used so that keys can be generated with
        'public_key' => '',
        // The secret keys generated by other tools may
        // need to be adjusted to match the input expected by libsodium.
        'private_key' => '',
        //
        // for strong security reason it should be less
        'session_ttl' => 15, // in minutes (TTL cannot be less then 10 minute)
        // you can reduce the time for higher security
        // for how long the token will be valid in the app.
        // in every "x" time the token will be refresh. 
        'token_validity' => 1, // in minutes
        // whether to check the IP and User Agent when the token is resolved.
        //
        'validation' => [
            'user_ip' => true,
            'user_agent' => true,
        ],
    ],
    'db' => [
        'driver'   => 'Pdo_Mysql',
        'driver_options' => [
            // PDO::ATTR_PERSISTENT => true,
            // https://www.php.net/manual/tr/mysqlinfo.concepts.buffering.php
            // 
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false,  // should be false

            // https://stackoverflow.com/questions/20079320/php-pdo-mysql-how-do-i-return-integer-and-numeric-columns-from-mysql-as-int
            // 
            PDO::ATTR_EMULATE_PREPARES => false,
            PDO::ATTR_STRINGIFY_FETCHES => false,

            // Enable exceptions
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        ],
        'database' => 'olobase_default',
        'hostname' => 'localhost',
        'username' => '',
        'password' => '',
        'platform' => 'Mysql',
    ],
    'redis' => [
        'host' => 'localhost',
        'port' => '6379',
        'timeout' => 60,
        'password' => '',
    ]
];

Ortamların konfigürasyonu hakkında daha detaylı bilgi için ortamlar bölümüne gözatabilirsiniz.

JWT Anahtarlarının Oluşturulması

Jwt kodlayıcı, belirteçleri imzalarken private_key ve belirteçleri okurken public_key olmak üzere iki genel ve özel anahtar kullanır. Her proje için farklı anahtarlar oluşturmanız gerekir. Bu bağlantıyı kullanarak genel ve özel anahtarlar oluşturabilirsiniz.

Varsayılan Veritabanının Yaratılması

olobase-skeleton-php projesi içerisinde default.sql adındaki dosyayı çalıştırmadan önce olobase_default adında bir veritabanı yaratın.

CREATE DATABASE olobase_default;

Daha sonra default.sql sql kodlarını bu veritabanı için çalıştırın.

Veritabanı Konfigürasyonu

Yukarıdaki örnek bağlantı Mysql sürücüsü içindir daha fazla örnek için laminas-db bağlantısını ziyaret edebilirsiniz.

Uygulamanıza Giriş

http://example.local/api/auth/token

Takip eden örnekte olduğu gibi Postman benzeri yazılımları kullanarak uygulamanızı test edebilirsiniz.

image info