Browse Source

added DB connection

Плотников Роман Вячеславович 3 years ago
parent
commit
d817e32c17
6 changed files with 50 additions and 7 deletions
  1. 6 5
      app/Controller/Site.php
  2. 3 1
      composer.json
  3. 11 0
      config/db.php
  4. 15 1
      core/Src/Application.php
  5. 4 0
      core/Src/Settings.php
  6. 11 0
      views/site/post.php

+ 6 - 5
app/Controller/Site.php

@@ -3,17 +3,18 @@
 namespace Controller;
 
 use Src\View;
+use Illuminate\Database\Capsule\Manager as DB;
 
 class Site
 {
     public function index(): string
     {
-        $view = new View();
-        return $view->render('site.hello', ['message' => 'index']);
+        $posts = DB::table('posts')->get();
+        return (new View())->render('site.post', ['posts' => $posts]);
     }
 
     public function hello(): string
     {
-        $view = new View();
-        return $view->render('site.hello', ['message' => 'hello']);    }
-}
+        return new View('site.hello', ['message' => 'hello working']);
+    }
+}

+ 3 - 1
composer.json

@@ -10,7 +10,9 @@
     ],
     "minimum-stability": "dev",
     "require": {
-        "php": "^7.4 | ^8.0"
+        "php": "^7.4 | ^8.0",
+        "illuminate/database": "10.x-dev",
+        "illuminate/events": "10.x-dev"
     },
     "autoload": {
         "psr-4": {

+ 11 - 0
config/db.php

@@ -0,0 +1,11 @@
+<?php
+return [
+   'driver' => 'mysql',
+   'host' => 'localhost',
+   'database' => 'mvc',
+   'username' => 'root',
+   'password' => '',
+   'charset' => 'utf8',
+   'collation' => 'utf8_unicode_ci',
+   'prefix' => '',
+];

+ 15 - 1
core/Src/Application.php

@@ -3,16 +3,21 @@
 namespace Src;
 
 use Error;
+use Illuminate\Container\Container;
+use Illuminate\Events\Dispatcher;
+use Illuminate\Database\Capsule\Manager as Capsule;
 
 class Application
 {
     private Settings $settings;
     private Route $route;
+    private Capsule $dbManager;
 
     public function __construct(Settings $settings)
     {
         $this->settings = $settings;
         $this->route = new Route();
+        $this->dbManager = new Capsule();
     }
 
     public function __get($key)
@@ -23,9 +28,18 @@ class Application
         throw new Error('Accessing a non-existent property');
     }
 
+    private function dbRun()
+    {
+        $this->dbManager->addConnection($this->settings->getDbSettings());
+        $this->dbManager->setEventDispatcher(new Dispatcher(new Container));
+        $this->dbManager->setAsGlobal();
+        $this->dbManager->bootEloquent();
+    }
+
     public function run(): void
     {
+        $this->dbRun();
         $this->route->setPrefix($this->settings->getRootPath());
         $this->route->start();
     }
-}
+}

+ 4 - 0
core/Src/Settings.php

@@ -30,4 +30,8 @@ class Settings
     {
         return '/' . $this->path['views'] ?? '';
     }
+
+    public function getDbSettings(): array {
+        return $this->db ?? [];
+    }
 }

+ 11 - 0
views/site/post.php

@@ -0,0 +1,11 @@
+<h1>Список статей</h1>
+<ol>
+   <?php
+   foreach ($posts as $post) { ?>
+       <li>
+           <p><?= $post->title ?></p>
+           <p><?= $post->text ?></p>
+           </li> <?php
+   }
+   ?>
+</ol>