ServBay में Slim प्रोजेक्ट बनाना और चलाना
यह लेख आपको दिखाएगा कि कैसे ServBay, एक शक्तिशाली लोकल वेब डिवेलपमेंट प्लेटफार्म, में तेजी से PHP-आधारित Slim Framework प्रोजेक्ट बनाएं, कॉन्फ़िगर करें और चलाएं। ServBay पहले से सेटअप PHP, वेब सर्वर (Caddy/Nginx/Apache), और कई डाटाबेस पैकेज प्रदान करता है, जिससे यह Slim डिवेलपमेंट के लिए उत्तम प्लेटफार्म बन जाता है।
Slim क्या है?
Slim एक हल्का PHP माइक्रो-फ्रेमवर्क है, जिसे साधारण लेकिन दमदार वेब ऐप्स और APIs को तेजी से बनाने के लिए डिज़ाइन किया गया है। यह कोर रूटिंग, रिक्वेस्ट और रिस्पॉन्स हैंडलिंग की सुविधा देता है—बिल्कुल उन प्रोजेक्ट्स के लिए उपयुक्त है जिन्हें तीव्रता से विकसित या डिप्लॉय करना हो, या जो बड़े ऐप के आधार के रूप में काम करें।
Slim की मुख्य विशेषताएँ और फायदे
- हल्का और तेज: Slim फ्रेमवर्क का कोर बहुत छोटा है, कम संसाधन लेता है, और जल्दी शुरू होता है, इसलिए छोट–मध्यम ऐप्स या माइक्रोसर्विस के लिए बढ़िया है।
- लचीलापन: Slim पूरी तरह से प्लगइनेबल है और किसी भी तीसरे पक्ष के टूल या लाइब्रेरी (जैसे टेम्प्लेट इंजन, ORM, ऑथेंटिकेशन) के साथ आसानी से जोड़ा जा सकता है, जिससे आप अपनी जरूरत के हिसाब से टूल चुन सकते हैं।
- यूज़र-फ्रेंडली: इसके सिंपल API और साफ़ डॉक्युमेंटेशन के चलते डिवेलपर्स जल्दी इसकी मूल बातों को समझ सकते हैं और काम शुरू कर सकते हैं।
- मजबूत रूटिंग: कई HTTP मेथड्स (GET, POST, PUT, DELETE आदि) और जटिल रूटिंग (रूट ग्रुप, मिडलवेयर, कंट्रोलर आदि) को सपोर्ट करता है।
- मिडलवेयर सहयोग: Slim में मिडलवेयर आपको जरूरी ऑपरेशन जैसे ऑथेंटिकेशन, लॉगिंग, या CORS हैंडलिंग, मुख्य ऐप कोड से पहले/बाद में लागू करने की सुविधा देता है।
Slim RESTful API बनाने, प्रोटोटाइपिंग, या खास फीचर वाली इंडिपेंडेंट सेवाओं के लिए आदर्श है।
ServBay में Slim प्रोजेक्ट बनाना और चलाना
यह गाइड ServBay के प्री-कॉन्फ़िगर्ड PHP एनवायरमेंट और वेबसाइट फीचर का उपयोग कर वेब सर्वर सेट करने और मिनटों में Slim प्रोजेक्ट रन करने में मदद देगा।
आवश्यकताएँ
शुरू करने से पहले, सुनिश्चित करें:
- ServBay इंस्टॉल और चालू करें: आपके macOS में ServBay इंस्टॉल और रनिंग हो।
- Composer ServBay में मौज़ूद है: ServBay में Composer पहले से मौजूद होता है, आपको अलग से इंस्टॉल करने की जरूरत नहीं।
Slim प्रोजेक्ट बनाएँ
ServBay सलाह देता है कि आप अपने सभी वेबसाइट प्रोजेक्ट्स /Applications/ServBay/www
फोल्डर में रखें, जिससे प्रोजेक्ट्स का प्रबंधन आसान हो जाता है।
- ServBay वेबसाइट रुट डायरेक्टरी में जाएँ:bash
cd /Applications/ServBay/www
1 - प्रोजेक्ट फोल्डर बनाएँ:bash
mkdir servbay-slim-app
1 - प्रोजेक्ट डायरेक्टरी में जाएँ:bash
cd servbay-slim-app
1 - Composer के साथ Slim इंस्टॉल करें:bashयह कमांड Slim Framework और
composer require slim/slim "^4.0" slim/psr7 -W
1slim/psr7
लाइब्रेरी आपके प्रोजेक्ट केvendor
फोल्डर में डाउनलोड करेगी औरcomposer.json
,composer.lock
फाइल बनाएगी।
Slim ऐप इनिशियलाइज़ करना
- एंट्री फाइल बनाएँ: आमतौर पर Slim प्रोजेक्ट के दवारा सभी रिक्वेस्ट हैंडल करने के लिए एक सिंगल एंट्री फाइल (जैसे
public/index.php
) इस्तेमाल की जाती है। प्रोजेक्ट रूट मेंpublic
फोल्डर बनाएँ और उसमेंindex.php
क्रिएट करें।bashmkdir public touch public/index.php
1
2 - एंट्री फाइल एडिट करें:
public/index.php
में बेसिक Slim ऐप कोड डालें:phpयह कोड एक बेसिक Slim ऐप बनाता है, जो ROOT URL (<?php // Composer ऑटोलोड फाइल लोड करें require __DIR__ . '/../vendor/autoload.php'; // जरूरी PSR-7 इंटरफेस और Slim फैक्ट्री क्लास को इम्पोर्ट करें use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; // Slim ऐप इंस्टेस बनाएँ $app = AppFactory::create(); // बेसिक रूट जोड़ें: GET रिक्वेस्ट के लिए '/' $app->get('/', function (Request $request, Response $response, $args) { // रिस्पांस बॉडी में टेक्स्ट भेजें $response->getBody()->write("Hello ServBay!"); // रिस्पांस ऑब्जेक्ट रिटर्न करें return $response; }); // Slim ऐप रन करें $app->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22/
) पर GET रिक्वेस्ट आने पर "Hello ServBay!" टेक्स्ट रिटर्न करेगा।
ServBay वेबसाइट कॉन्फ़िगर करें
अपने Slim प्रोजेक्ट को ब्राउज़र में एक्सेस करने के लिए, आपको ServBay में एक वेबसाइट (पुराने वर्शन में "host") कॉन्फ़िगर करनी होगी।
- ServBay ऐप ओपन करें।
- Websites फीचर में जाएँ।
- नई वेबसाइट जोड़ें।
- कॉन्फ़िगरेशन में:
- नाम (Name):
My First Slim Dev Site
(या अपनी पसंद का नाम) - डोमेन (Domain):
servbay-slim-test.local
(लोकल डेव के लिए.local
या.test
एंडिंग का इस्तेमाल करें) - वेबसाइट टाइप (Website Type):
PHP
- PHP वर्शन (PHP Version): अपनी जरूरत अनुसार (जैसे
8.3
) - डॉक्युमेंट रूट (Document Root): ब्राउज़ बटन दबाकर अपने प्रोजेक्ट में
public
फोल्डर को सेलेक्ट करें, यानी/Applications/ServBay/www/servbay-slim-app/public
(Slim का एंट्री फाइलpublic
में है, इसलिए सर्वर को इसी डायरेक्टरी पर पॉइंट करना चाहिए)।
- नाम (Name):
- वेबसाइट सेव करें। ServBay सर्वर कॉन्फ़िगरेशन खुद अपडेट और लागू करेगा।
पहली वेबसाइट जोड़ना गाइड देखें यदि विस्तार से जानना हो।
अपना Slim वेबसाइट विज़िट करें
अब अपना ब्राउज़र ओपन करें और सेट किया डोमेन https://servbay-slim-test.local
खोलें।
अगर सब सही रहा, तो ब्राउज़र पेज पर Hello ServBay!
नजर आना चाहिए। इसका मतलब आपका Slim प्रोजेक्ट सर्वर पर ठीक से चल रहा है।
डाटाबेस इंटीग्रेशन के उदाहरण
Slim Framework में डाटाबेस अब्स्ट्रैक्शन लेयर नहीं है, पर PHP की किसी भी डाटाबेस लाइब्रेरी को आराम से जोड़ा जा सकता है। नीचे हम Laravel के Eloquent ORM (illuminate/database
) का उपयोग कर MySQL, PostgreSQL को कनेक्ट करना और Memcached, Redis का इंटीग्रेशन दिखा रहे हैं।
आवश्यक शर्त: डाटाबेस क्रिएट करें और माइग्रेशन रन करें
डाटाबेस इंटीग्रेशन से पहले, ServBay में डाटाबेस बनाएं और जरूरी टेबल स्ट्रक्चर सेट करें।
- डाटाबेस बनाएँ:
- ServBay ऐप खोलें, डाटाबेस software package (जैसे MySQL या PostgreSQL) में जाएँ।
- phpMyAdmin (MySQL/MariaDB के लिए), pgAdmin (PostgreSQL के लिए), या कमांडलाइन टूल से एक नया डाटाबेस जैसे
servbay_slim_app
बनाएं। - आमतौर पर डिफ़ॉल्ट डाटाबेस root यूज़र का पासवर्ड
password
होता है, इसे ServBay इंटरफेस में देख/बदल सकते हैं।
- Phinx (डाटाबेस माइग्रेशन टूल) इंस्टॉल करे और सेटअप करें:
- प्रोजेक्ट रूट
/Applications/ServBay/www/servbay-slim-app
में Composer द्वारा Phinx इंस्टॉल करें:bashcomposer require robmorgan/phinx
1 - Phinx कॉन्फ़िगरेशन इनिशियलाइज़ करें:bashइससे प्रोजेक्ट रूट में
vendor/bin/phinx init
1phinx.yml
फाइल बनेगी। उसमें अपनी डाटाबेस डिटेल अपडेट करें:yamlpaths: migrations: '%%PHINX_CONFIG_DIR%%/db/migrations' seeds: '%%PHINX_CONFIG_DIR%%/db/seeds' environments: default_migration_table: phinxlog default_environment: development development: adapter: mysql # या pgsql host: 127.0.0.1 name: servbay_slim_app user: root pass: password port: 3306 # MySQL के लिए, PostgreSQL के लिए 5432 charset: utf8mb4 collation: utf8mb4_unicode_ci version_order: creation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
- प्रोजेक्ट रूट
- माइग्रेशन फाइल बनाएँ: नई माइग्रेशन फाइल बनाएं।bashइससे
vendor/bin/phinx create CreateUsersTable
1db/migrations
में PHP फाइल बनेगी। उसमेंchange()
मेथड मेंusers
टेबल डिफाइन करें:php<?php declare(strict_types=1); use Phinx\Migration\AbstractMigration; final class CreateUsersTable extends AbstractMigration { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change(): void { $table = $this->table('users'); $table->addColumn('name', 'string') ->addColumn('email', 'string', ['unique' => true]) ->addTimestamps() // created_at और updated_at फील्ड जोड़ें ->create(); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 - माइग्रेशन रन करें: प्रोजेक्ट रूट में Phinx चलाकर
users
टेबल बनाएँ।bashमहत्वपूर्ण: नीचे का कोई भी डाटाबेस कोड रन करने से पहले ये स्टेप्स जरूर पूरे करें।vendor/bin/phinx migrate
1
illuminate/database (Eloquent ORM) का उपयोग करते हुए
Laravel का ORM (illuminate/database
) इस्तेमाल करें:
illuminate/database इंस्टॉल करें:
bashcomposer require illuminate/database
1public/index.php
में डाटाबेस कनेक्शन इनिशियलाइज़ करें:require __DIR__ . '/../vendor/autoload.php';
के बाद, और Slim ऐप ($app = AppFactory::create();
) से पहले, ये कोड डालें:php// ... अन्य require और use स्टेटमेंट्स ... use Illuminate\Database\Capsule\Manager as Capsule; // Capsule मैनेजर को इम्पोर्ट करें // Eloquent ORM इनिशियलाइज़ करें $capsule = new Capsule; // डाटाबेस कनेक्शन कॉन्फ़िगर करें (MySQL या pgsql अनुसार बदलें) $capsule->addConnection([ 'driver' => 'mysql', // या 'pgsql' 'host' => '127.0.0.1', 'database' => 'servbay_slim_app', 'username' => 'root', 'password' => 'password', 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', // PostgreSQL के लिए schema पैरामीटर जोड़ें // 'schema' => 'public', ]); // ग्लोबली एक्सेसिबल सेट करें $capsule->setAsGlobal(); // Eloquent शुरू करें $capsule->bootEloquent(); // ... (Slim ऐप बनाएँ) ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
MySQL उदाहरण
माना आप ServBay में MySQL चला रहे हैं, और servbay_slim_app
डाटाबेस + users
टेबल बने हैं।
public/index.php
में $app->run();
से पहले ये रूट जोड़ें:
php
// ... पहले का इनिशियलाइजेशन और '/' रूट ...
use Illuminate\Database\Capsule\Manager as Capsule; // सुनिश्चित करें इम्पोर्टेड है
// नया यूजर जोड़ने का रूट
$app->get('/mysql-add-user', function (Request $request, Response $response, $args) {
try {
Capsule::table('users')->insert([
'name' => 'ServBay Demo User',
'email' => 'servbay-demo-' . time() . '@servbay.test',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
$response->getBody()->write('User added to MySQL');
} catch (\Exception $e) {
$response->getBody()->write('Error adding user: ' . $e->getMessage());
$response = $response->withStatus(500);
}
return $response;
});
// सभी यूजर्स को पाने का रूट
$app->get('/mysql-get-users', function (Request $request, Response $response, $args) {
try {
$users = Capsule::table('users')->get();
$response->getBody()->write($users->toJson());
$response = $response->withHeader('Content-Type', 'application/json');
} catch (\Exception $e) {
$response->getBody()->write('Error fetching users: ' . $e->getMessage());
$response = $response->withStatus(500);
}
return $response;
});
// ... $app->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ऐक्सेस करें:
https://servbay-slim-test.local/mysql-add-user
चलाने से एक नया यूज़र जुड़ जाएगा।https://servbay-slim-test.local/mysql-get-users
पर सारे यूज़र JSON में मिलेंगे।
PostgreSQL उदाहरण
मान लीजिए आपने ServBay में PostgreSQL शुरू किया, डाटाबेस बनाया और माइग्रेशन चला दिया।
public/index.php
में कनेक्शन सेटिंग बदलें:
php
$capsule->addConnection([
'driver' => 'pgsql',
'host' => '127.0.0.1',
'database' => 'servbay_slim_app',
'username' => 'root',
'password' => 'password',
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
]);
// ... बाकी Eloquent कोड जस का तस ...
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
फिर $app->run();
से पहले ये रूट जोड़ें:
php
// ... पहले का इनिशियलाइजेशन और '/' रूट ...
// ... MySQL रूट (अगर जरूरत हो) ...
use Illuminate\Database\Capsule\Manager as Capsule;
// नया यूज़र जोड़ने का रूट
$app->get('/pgsql-add-user', function (Request $request, Response $response, $args) {
try {
Capsule::table('users')->insert([
'name' => 'ServBay PG Demo User',
'email' => 'servbay-pg-demo-' . time() . '@servbay.test',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
$response->getBody()->write('User added to PostgreSQL');
} catch (\Exception $e) {
$response->getBody()->write('Error adding user: ' . $e->getMessage());
$response = $response->withStatus(500);
}
return $response;
});
// सभी यूज़र पाने का रूट
$app->get('/pgsql-get-users', function (Request $request, Response $response, $args) {
try {
$users = Capsule::table('users')->get();
$response->getBody()->write($users->toJson());
$response = $response->withHeader('Content-Type', 'application/json');
} catch (\Exception $e) {
$response->getBody()->write('Error fetching users: ' . $e->getMessage());
$response = $response->withStatus(500);
}
return $response;
});
// ... $app->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
ऐक्सेस करें:
https://servbay-slim-test.local/pgsql-add-user
से PostgreSQL में नया यूज़र जुड़ जाएगा।https://servbay-slim-test.local/pgsql-get-users
पर सभी यूज़र JSON में मिलेंगे।
Memcached उदाहरण
ServBay Memcached पैकेज और PHP के ext-memcached
एक्सटेंशन के साथ आता है। बस Composer से PHP क्लाइंट लाइब्रेरी इंस्टॉल करें—यहाँ हम memcached/memcached
का प्रयोग कर रहे हैं।
Memcached क्लाइंट इंस्टॉल करें:
bashcomposer require memcached/memcached
1public/index.php
में Memcached रूट जोड़ें:php// ... सभी इनिशियलाइजेशन और डाटाबेस रूट ... // Memcached के लिए रूट $app->get('/memcached-example', function (Request $request, Response $response, $args) { // Memcached क्लाइंट इनिशियलाइज करें $memcached = new Memcached(); // सर्वर जोड़ें (ServBay का डिफ़ॉल्ट 127.0.0.1:11211) $memcached->addServer('127.0.0.1', 11211); $cacheKey = 'my_servbay_cache_key'; // कैश से डेटा प्राप्त करें $cachedData = $memcached->get($cacheKey); if ($cachedData === false) { // कैश खाली है: डेटा बनाकर सेव करें $cachedData = 'Hello Memcached from ServBay! This was not cached.'; $memcached->set($cacheKey, $cachedData, 60); $response->getBody()->write($cachedData); } else { // cache हिट $response->getBody()->write('Hello Memcached from ServBay! This was served from cache.'); } return $response; }); // ... $app->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ऐक्सेस:https://servbay-slim-test.local/memcached-example
खोलने पर पहली बार "This was not cached." दिखेगा, और बाद की विज़िट्स (कैश खत्म होने से पहले) में "This was served from cache." मिलेगा।
Redis उदाहरण
ServBay Redis पैकेज और PHP के ext-redis
एक्सटेंशन के साथ आता है। Composer से PHP क्लाइंट लाइब्रेरी (predis/predis
) इंस्टॉल करें।
Redis क्लाइंट इंस्टॉल करें:
bashcomposer require predis/predis
1public/index.php
में Redis के रूट जोड़ें:php// ... इनिशियलाइजेशन और डाटाबेस, Memcached रूट्स ... use Predis\Client as RedisClient; // Predis क्लाइंट इम्पोर्ट करें // Redis के लिए रूट $app->get('/redis-example', function (Request $request, Response $response, $args) { try { // Redis क्लाइंट बनाएँ (ServBay पर 127.0.0.1:6379 डिफ़ॉल्ट) $redis = new RedisClient([ 'scheme' => 'tcp', 'host' => '127.0.0.1', 'port' => 6379, ]); $cacheKey = 'my_servbay_redis_cache_key'; // डेटा प्राप्त करें $cachedData = $redis->get($cacheKey); if ($cachedData === null) { // अगर डेटा नहीं, तो नया डेटा डालें $cachedData = 'Hello Redis from ServBay! This was not cached.'; $redis->setex($cacheKey, 60, $cachedData); $response->getBody()->write($cachedData); } else { // cache हिट $response->getBody()->write('Hello Redis from ServBay! This was served from cache.'); } } catch (\Exception $e) { // कनेक्शन या ऑपरेशन एरर $response->getBody()->write('Error connecting to Redis or performing operation: ' . $e->getMessage()); $response = $response->withStatus(500); } return $response; }); // ... $app->run();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
ऐक्सेस: https://servbay-slim-test.local/redis-example
पर पहली बार "This was not cached." आएगा और बाद में (कैश रहते) "This was served from cache."।
निष्कर्ष
ऊपर दिए गए स्टेप्स फॉलो करके, आपने ServBay लोकल डिवेलपमेंट एनवायरमेंट में Slim Framework प्रोजेक्ट सक्सेसफुली तैयार कर लिया है और वेबसाइट फीचर से उसे होस्ट/एक्सेस किया। आपने ServBay के दिए गए विभिन्न पैकेजेज़ (MySQL, PostgreSQL, Memcached, Redis आदि) और रिलेटेड PHP एक्सटेंशन का इस्तेमाल कर डाटाबेस और कैशिंग को अपने Slim ऐप में जोड़ना भी सीख लिया। ServBay आपकी लोकल डेवलपमेंट की झंझटें कम कर देता है ताकि आप कोर Slim ऐप डिवेलपमेंट पर ध्यान केंद्रित रख सको।