Skip to content

Commit 71c14ba

Browse files
committed
Template for initializing the PHP MVC Project application using Bootstrap v4.
0 parents  commit 71c14ba

25 files changed

+654
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/*.lock
3+
/cache

.htaccess

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine On
3+
4+
# redirect /index.php to /
5+
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php
6+
RewriteRule ^index.php/?(.*)$ $1 [R=301,L]
7+
8+
# process all requests through index.php, except for actually existing files
9+
RewriteCond %{REQUEST_FILENAME} !-d
10+
RewriteCond %{REQUEST_FILENAME} !-f
11+
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [QSA,L]
12+
</IfModule>

.vscode/launch.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Listen for XDebug",
6+
"type": "php",
7+
"request": "launch",
8+
"port": 9000,
9+
"log": true,
10+
"cwd": "${workspaceRoot}"
11+
},
12+
{
13+
"name": "Launch currently open script",
14+
"type": "php",
15+
"request": "launch",
16+
"program": "${file}",
17+
"cwd": "${fileDirname}",
18+
"port": 9000
19+
}
20+
]
21+
}

.vscode/tasks.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
// See https://go.microsoft.com/fwlink/?LinkId=733558
3+
// for the documentation about the tasks.json format
4+
"version": "2.0.0",
5+
"tasks": [
6+
{
7+
"label": "PHP Server",
8+
"type": "shell",
9+
"command": "php -S localhost:8000"
10+
}
11+
]
12+
}

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Bootstrap 4 Template
2+
3+
Template for initializing the PHP MVC Project application using Bootstrap v4.
4+
5+
The template includes a standard directory structure MVC, contains a samples of controllers and view files, and includes layout Bootstrap based.
6+
7+
Also, the template will include a filter for error handling.
8+
9+
[Demo](http://template-bootstrap4.rf.gd/)
10+
11+
## Installation
12+
13+
```
14+
git clone https://github.com/php-mvc-project/template-bootstrap4.git my-app
15+
cd my-app
16+
composer install
17+
```
18+
19+
## Requirements
20+
21+
* PHP 7.x
22+
* Composer
23+
24+
## License
25+
26+
The MIT License (MIT)
27+
28+
Copyright © 2018, [@meet-aleksey](https://github.com/meet-aleksey)

composer.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "php-mvc-project/template-bootstrap4",
3+
"description": "Template for initializing the PHP MVC Project application using Bootstrap v4.",
4+
"keywords": ["php mvc project", "templates", "template", "mvc"],
5+
"license": "MIT",
6+
"type": "project",
7+
"authors": [
8+
{
9+
"name": "Meet Aleksey",
10+
"homepage": "https://github.com/meet-aleksey"
11+
}
12+
],
13+
"require": {
14+
"php-mvc-project/php-mvc": "^1.1.1",
15+
"components/jquery": "3.3.1",
16+
"twbs/bootstrap": "4.1.1"
17+
}
18+
}

content/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Use this folder for static files.

content/images/php-mvc-logo.png

25.2 KB
Loading

content/styles/app.css

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
header {
2+
margin-bottom: 24px;
3+
}
4+
5+
.set-colon-to-labels label::after {
6+
content: ':';
7+
}

controllers/AccountController.php

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
// this controller shows an example of using models
3+
4+
// make sure you add the Controllers child name to the root namespace of your application
5+
namespace RootNamespaceOfYourApp\Controllers;
6+
7+
// import the base class of the controller
8+
use PhpMvc\Controller;
9+
// import class for model annotation
10+
use PhpMvc\Model;
11+
12+
// expand your class with the base controller class
13+
class AccountController extends Controller {
14+
15+
// create here action methods for the views of this controller
16+
// the action methods must have a modifier public
17+
18+
public function __construct() {
19+
// set model type for actions
20+
Model::use(array('index', 'login'), 'Login');
21+
22+
// required fields
23+
Model::required('Login', 'username');
24+
Model::required('Login', 'password');
25+
26+
// display name (for <label />) and text
27+
Model::display('Login', 'username', 'Username or Email', 'Enter any username.');
28+
Model::display('Login', 'password', 'Password', 'Enter 123.');
29+
30+
// custom validation
31+
Model::validation('Login', 'password', function($value, &$errorMessage) {
32+
if ($value != '123') {
33+
$errorMessage = 'Expected value is 123';
34+
return false;
35+
}
36+
37+
return true;
38+
});
39+
}
40+
41+
// default action
42+
// url: /account or /account/index
43+
public function index() {
44+
if (isset($this->getSession()['user'])) {
45+
// is user, redirect to home
46+
return $this->redirectToAction('index', 'home');
47+
}
48+
49+
return $this->view();
50+
}
51+
52+
// login action, only for POST requests
53+
// because the $model is required and object
54+
// url: /account/login
55+
public function login(\RootNamespaceOfYourApp\Models\Login $model) {
56+
if (!$this->getModelState()->isValid()) {
57+
// model is not valid, return login form
58+
return $this->view('index', $model);
59+
}
60+
61+
// model is valid, create session
62+
$session = $this->getSession();
63+
64+
$session['user'] = $model->username;
65+
66+
// redirect to home
67+
return $this->redirectToAction('index', 'home');
68+
}
69+
70+
// logout action
71+
// url: /account/logout
72+
public function logout() {
73+
$this->getSession()->clear();
74+
return $this->redirectToAction('index', 'home');
75+
}
76+
77+
}

controllers/HomeController.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
// make sure you add the Controllers child name to the root namespace of your application
3+
namespace RootNamespaceOfYourApp\Controllers;
4+
5+
// import the base class of the controller
6+
use PhpMvc\Controller;
7+
8+
// expand your class with the base controller class
9+
class HomeController extends Controller {
10+
11+
// create here action methods for the views of this controller
12+
// the action methods must have a modifier public
13+
14+
public function index() {
15+
return $this->view();
16+
}
17+
18+
public function about() {
19+
return $this->view();
20+
}
21+
22+
public function contact() {
23+
return $this->view();
24+
}
25+
26+
// methods of action can return not only views, but also any other data
27+
// use the addresses below to look at the result of the following action:
28+
// /home/example/content
29+
// /home/example/json
30+
// /home/example/file
31+
// /home/example/view
32+
public function example($id = null) {
33+
switch ($id) {
34+
case 'content':
35+
return $this->content('Text plain');
36+
37+
case 'json':
38+
return $this->json(array('message' => 'This is the JSON data. You can use an array or pass an instance of the object.'));
39+
40+
case 'file':
41+
return $this->file('~/content/images/php-mvc-logo.png');
42+
43+
default:
44+
return $this->view('index');
45+
}
46+
}
47+
48+
}

controllers/README.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Create a controllers classes in this folder.
2+
3+
Remember that the controller name must end with the **Controller** suffix.
4+
5+
The controllers classes must be part of the `Controllers` namespace of the root namespace of your application.
6+
7+
The controllers classes must be inherited from the `PhpMvc\Controller` class.
8+
9+
For example:
10+
11+
```php
12+
<?php
13+
// make sure you add the Controllers child name to the root namespace of your application
14+
namespace RootNamespaceOfYourApp\Controllers;
15+
16+
// import the base class of the controller
17+
use PhpMvc\Controller;
18+
19+
// expand your class with the base controller class
20+
class HomeController extends Controller {
21+
22+
public function index() {
23+
// use the content function to return text content:
24+
return $this->content('Hello, world!');
25+
26+
// create to the ./view/home/index.php
27+
// and use view function to return this view:
28+
// return $this->view();
29+
}
30+
31+
}
32+
```

filters/HandleError.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace RootNamespaceOfYourApp\Filters;
3+
4+
use PhpMvc\ActionFilter;
5+
use PhpMvc\ViewResult;
6+
7+
class HandleError extends ActionFilter {
8+
9+
/**
10+
* Called when an exception occurs.
11+
*
12+
* @param ExceptionContext $exceptionContext The context of action exception.
13+
*
14+
* @return void
15+
*/
16+
public function exception($exceptionContext) {
17+
$exceptionContext->setResult(new ViewResult('error'));
18+
}
19+
20+
}

filters/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Create a fliters classes in this folder.
2+
3+
The filters must be part of the `Filters` namespace of the root namespace of your application.
4+
5+
The filters must be inherited from the `PhpMvc\ActionFilter` class.
6+
7+
For example:
8+
9+
```php
10+
<?php
11+
// make sure you add the Filters child name to the root namespace of your application
12+
namespace RootNamespaceOfYourApp\Filters;
13+
14+
// import the base class of the action filter
15+
use PhpMvc\ActionFilter;
16+
17+
class TestFilter extends ActionFilter {
18+
19+
// action executed handler
20+
public function actionExecuted($actionExecutedContext) {
21+
$actionExecutedContext->setResult(new PhpMvc\ContentResult('executed'));
22+
}
23+
24+
}
25+
```

0 commit comments

Comments
 (0)