diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..68aac31 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.git +runtime \ No newline at end of file diff --git a/.env.example b/.env.example index 512046a..dee47ba 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ -DB_DSN = mysql:host=localhost;port=3306;dbname=mvc_framework -DB_USER = root -DB_PASSWORD = \ No newline at end of file +DB_DSN = mysql:host=db;port=3306;dbname=php_mvc +DB_USER = php_mvc +DB_PASSWORD = php_mvc \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3070064..8e5a570 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea vendor composer.lock -.env \ No newline at end of file +.env +.vscode \ No newline at end of file diff --git a/README.md b/README.md index d2d3bc2..3c025b9 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,17 @@ Minimalistic custom framework created for educational purposes. 7. Start php server by running command `php -S 127.0.0.1:8080` 8. Open in browser http://127.0.0.1:8080 +------ +## Installation using docker +Make sure you have docker installed. To see how you can install docker on Windows [click here](https://youtu.be/2ezNqqaSjq8).
+Make sure `docker` and `docker-compose` commands are available in command line. + +1. Clone the project using git +1. Copy `.env.example` into `.env` (Don't need to change anything for local development) +1. Navigate to the project root directory and run `docker-compose up -d` +1. Install dependencies - `docker-compose exec app composer install` +1. Run migrations - `docker-compose exec app php migrations.php` +8. Open in browser http://127.0.0.1:8080 > The project was created along with Youtube Video Series "[Build PHP MVC Framework](https://www.youtube.com/playlist?list=PLLQuc_7jk__Uk_QnJMPndbdKECcTEwTA1)". > I appreaciate if you share it. diff --git a/composer.json b/composer.json index ff794d0..12a7b15 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,6 @@ }, "require": { "vlucas/phpdotenv": "^5.0", - "thecodeholic/php-mvc-core": "^v1.0.4" + "thecodeholic/php-mvc-core": "^v1.0.5" } } diff --git a/controllers/SiteController.php b/controllers/SiteController.php index bb4ee04..fdb79b4 100644 --- a/controllers/SiteController.php +++ b/controllers/SiteController.php @@ -38,6 +38,9 @@ public function home() public function login(Request $request) { + echo '
';
+        var_dump($request->getBody(), $request->getRouteParam('id'));
+        echo '
'; $loginForm = new LoginForm(); if ($request->getMethod() === 'post') { $loginForm->loadData($request->getBody()); @@ -85,4 +88,11 @@ public function profile() { return $this->render('profile'); } -} \ No newline at end of file + + public function profileWithId(Request $request) + { + echo '
';
+        var_dump($request->getBody());
+        echo '
'; + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..8f1a64c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: "3.7" + +services: + app: + build: ./docker + image: thecodeholic/php_mvc + ports: + - "8080:80" + volumes: + # Mount source-code for development + - ./:/var/www + extra_hosts: + - host.docker.internal:host-gateway + + db: + image: mysql:8 + ports: + - "3307:3306" + volumes: + - mysql-data:/var/lib/mysql + - ./docker/mysql-config.cnf:/etc/mysql/conf.d/config.cnf + environment: + MYSQL_ROOT_PASSWORD: root + MYSQL_DATABASE: php_mvc + MYSQL_USER: php_mvc + MYSQL_PASSWORD: php_mvc + +volumes: + mysql-data: \ No newline at end of file diff --git a/docker/000-default.conf b/docker/000-default.conf new file mode 100644 index 0000000..3b74b4b --- /dev/null +++ b/docker/000-default.conf @@ -0,0 +1,10 @@ + + ServerAdmin webmaster@localhost + DocumentRoot /var/www/public + + + Options Indexes FollowSymLinks + AllowOverride All + Require all granted + + \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..afe41e5 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,47 @@ +FROM php:8.1-apache + +# Copy virtual host into container +COPY 000-default.conf /etc/apache2/sites-available/000-default.conf + +# Enable rewrite mode +RUN a2enmod rewrite + +# Install necessary packages +RUN apt-get update && \ + apt-get install \ + libzip-dev \ + wget \ + git \ + unzip \ + -y --no-install-recommends + +# Install PHP Extensions +RUN docker-php-ext-install zip pdo_mysql + +RUN pecl install -o -f xdebug-3.1.5 \ + && docker-php-ext-enable xdebug +# && rm -rf /tmp/pear + +# Copy composer installable +COPY ./install-composer.sh ./ + +# Copy php.ini +COPY ./php.ini /usr/local/etc/php/ + +# Cleanup packages and install composer +RUN apt-get purge -y g++ \ + && apt-get autoremove -y \ + && rm -r /var/lib/apt/lists/* \ + && rm -rf /tmp/* \ + && sh ./install-composer.sh \ + && rm ./install-composer.sh + +# Change the current working directory +WORKDIR /var/www + +# Change the owner of the container document root +RUN chown -R www-data:www-data /var/www + +# Start Apache in foreground +CMD ["apache2-foreground"] + diff --git a/docker/install-composer.sh b/docker/install-composer.sh new file mode 100644 index 0000000..bcb1e7a --- /dev/null +++ b/docker/install-composer.sh @@ -0,0 +1,17 @@ +#!/bin/sh + +php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" +SIGNATURE=$(php -r "echo hash_file('SHA384', 'composer-setup.php');") +EXPECTED_SIGNATURE=$(wget -q -O - https://composer.github.io/installer.sig) + +if [ "$EXPECTED_SIGNATURE" != "$SIGNATURE" ] +then + echo 'ERROR: Invalid installer signature' + rm composer-setup.php + exit 1 +fi + +php composer-setup.php --quiet --install-dir=/usr/local/bin --filename=composer +RESULT=$? +rm composer-setup.php +exit $RESULT \ No newline at end of file diff --git a/docker/mysql-config.cnf b/docker/mysql-config.cnf new file mode 100644 index 0000000..1f3bb11 --- /dev/null +++ b/docker/mysql-config.cnf @@ -0,0 +1,11 @@ +[client] +default-character-set = utf8mb4 + +[mysql] +default-character-set = utf8mb4 + +[mysqld] +init-connect='SET NAMES utf8mb4' +collation_server=utf8mb4_unicode_ci +character_set_server=utf8mb4 +default_authentication_plugin= mysql_native_password \ No newline at end of file diff --git a/docker/php.ini b/docker/php.ini new file mode 100644 index 0000000..dd15c37 --- /dev/null +++ b/docker/php.ini @@ -0,0 +1,8 @@ +; General +upload_max_filesize = 200M +post_max_size = 220M + +[xdebug] +xdebug.mode = debug +xdebug.start_with_request = yes +xdebug.client_host = host.docker.internal \ No newline at end of file diff --git a/public/index.php b/public/index.php index 33d2be1..8572ddf 100644 --- a/public/index.php +++ b/public/index.php @@ -32,10 +32,19 @@ $app->router->get('/register', [SiteController::class, 'register']); $app->router->post('/register', [SiteController::class, 'register']); $app->router->get('/login', [SiteController::class, 'login']); +$app->router->get('/login/{id}', [SiteController::class, 'login']); $app->router->post('/login', [SiteController::class, 'login']); $app->router->get('/logout', [SiteController::class, 'logout']); $app->router->get('/contact', [SiteController::class, 'contact']); $app->router->get('/about', [AboutController::class, 'index']); $app->router->get('/profile', [SiteController::class, 'profile']); +$app->router->get('/profile/{id:\d+}/{username}', [SiteController::class, 'login']); +// /profile/{id} +// /profile/13 +// \/profile\/\w+ -$app->run(); \ No newline at end of file +// /profile/{id}/zura +// /profile/12/zura + +// /{id} +$app->run();