Skip to content

Commit 6dd0baf

Browse files
committed
v2 beta
1 parent b30f6a4 commit 6dd0baf

File tree

31 files changed

+2439
-627
lines changed

31 files changed

+2439
-627
lines changed

app/bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php
2-
require_once __DIR__.'./../vendor/autoload.php';
3-
include_once __DIR__.'./config/config.php';
1+
<?php
2+
require_once __DIR__.'./../vendor/autoload.php';
3+
include_once __DIR__.'./config/config.php';
4+
include_once __DIR__.'./core/helpers/Session.php';
5+
include_once __DIR__.'./core/helpers/others.php';
46
include __DIR__.'/routes/web.php';

app/components/form/BasicForm.php

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
<?php
2-
3-
namespace app\components\form;
4-
5-
use app\core\Model;
6-
7-
class BasicForm
8-
{
9-
public static function start($method, $attrs=[],$action=null)
10-
{
11-
12-
if ($action==null){
13-
$action = '';
14-
}
15-
$addAttributes ='';
16-
foreach ($attrs as $key =>$value){
17-
$addAttributes =$addAttributes.$key.'="'.$value.'" ';
18-
}
19-
//echo $addAttributes;
20-
echo sprintf('<form action="%s" method="%s" %s>',$action,$method,$addAttributes);
21-
22-
}
23-
public static function end(){
24-
echo '</form>';
25-
}
26-
public static function input(Model $model,$name, $errorClass, $attrs=[], $type=null ){
27-
if ($type==null){
28-
$type = 'text';
29-
}
30-
$addAttributes ='';
31-
32-
$errorClass = $model->hasError($name)?$errorClass:" ";
33-
$class=$errorClass.' ';
34-
foreach ($attrs as $key =>$value){
35-
if ($key == 'class'){
36-
$class = $class.$value.' ';
37-
}
38-
else{
39-
$addAttributes =$addAttributes.$key.'="'.$value.'" ';
40-
}
41-
}
42-
//echo nl2br($class);
43-
echo sprintf('<input type="%s" value="%s" class="%s" name="%s" %s>',$type,$model->$name,$class,$name,$addAttributes);
44-
}
45-
46-
public static function error(Model $model,$errorFor)
47-
{
48-
echo $model->firstErr($errorFor);
49-
}
1+
<?php
2+
3+
namespace app\components\form;
4+
5+
use app\core\Model;
6+
7+
class BasicForm
8+
{
9+
public static function start($method, $attrs=[],$action=null)
10+
{
11+
12+
if ($action==null){
13+
$action = '';
14+
}
15+
$addAttributes ='';
16+
foreach ($attrs as $key =>$value){
17+
$addAttributes =$addAttributes.$key.'="'.$value.'" ';
18+
}
19+
//echo $addAttributes;
20+
echo sprintf('<form action="%s" method="%s" %s>',$action,$method,$addAttributes);
21+
22+
}
23+
public static function end(){
24+
echo '</form>';
25+
}
26+
public static function input(Model $model,$name, $errorClass, $attrs=[], $type=null,$values=null ): void
27+
{
28+
if ($type==null){
29+
$type = 'text';
30+
}
31+
$addAttributes ='';
32+
33+
$errorClass = $model->hasError($name)?$errorClass:" ";
34+
$class=$errorClass.' ';
35+
foreach ($attrs as $key =>$value){
36+
if ($key == 'class'){
37+
$class = $class.$value.' ';
38+
}
39+
else{
40+
$addAttributes =$addAttributes.$key.'="'.$value.'" ';
41+
}
42+
}
43+
//echo nl2br($class);
44+
echo sprintf('<input type="%s" value="%s" class="%s" name="%s" %s>',$type,empty($model->$name)?$values:$model->$name,$class,$name,$addAttributes);
45+
}
46+
47+
public static function error(Model $model,$errorFor)
48+
{
49+
echo $model->firstErr($errorFor);
50+
}
5051
}

app/config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
define( 'PUBDIR', dirname(__DIR__,2).'/public' );
3+
4+
25
const TITLE = 'NMVC- SITE TITLE';
36
const DESCRIPTION = 'NMVC is a php MVC framework that can do handel you basic job very well';
47
const KEYWORDS = ['nmvc','ishaf'];

app/controllers/Pages.php

Lines changed: 102 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,139 @@
22

33
namespace app\controllers;
44

5+
use app\core\App;
56
use app\core\Controller;
7+
use app\core\Exceptions\NotFound;
68
use app\core\Request;
7-
use app\middlewares\authMiddleware;
89
use app\models\Test;
910

1011
class Pages extends Controller
1112
{
13+
public NotFound $exception;
14+
private Test $model;
15+
1216
public function __construct()
1317
{
18+
$this->model = new Test();
19+
$this->exception =new NotFound();
1420
parent::__construct();
1521
//$this->middleware(new authMiddleware(['index']));
1622
}
1723

24+
/**
25+
* @throws Created
26+
*/
1827
public function index(Request $request){
1928

20-
$model = new Test();
29+
2130
if ($request->isGet()){
2231
$data =[
23-
'dfsdf'=>$_ENV['DB_NAME'],
24-
'model'=>$model,
32+
'data'=>$this->model->all(),
33+
'model'=>$this->model,
2534
'metadata'=>[
2635
'title'=>'Hello page',
2736
'description'=>'Here is my page description',
2837
'keywords'=>['test','hello'],
2938
'author'=>'ishaf',
3039
]
3140
];
32-
$this->view('index',$data);
41+
$this->view('test/index',$data);
3342
}
43+
44+
}
45+
public function store(Request $request){
3446
if ($request->isPost()){
35-
$model->loaddata($request->getBody());
36-
$model->validate();
37-
//var_dump($model->errors);
38-
if ($model->validate()){
39-
echo 'success';
40-
var_export($model);
47+
$this->model->loaddata($request->getBody());
48+
$this->model->validate();
49+
if ($this->model->validate() && $this->model->register()){
50+
App::$app->flashMessage->setFlash('success','user created','/test');
4151
}
4252
$data =[
43-
'model'=>$model
53+
'model'=>$this->model,
54+
];
55+
$this->view('test/create',$data);
56+
}
57+
}
58+
public function create(Request $request){
59+
if ($request->isGet()){
60+
$data =[
61+
'model'=>$this->model,
62+
'metadata'=>[
63+
'title'=>'Hello page',
64+
'description'=>'Here is my page description',
65+
'keywords'=>['test','hello'],
66+
'author'=>'ishaf',
67+
]
4468
];
45-
$this->view('index',$data);
69+
$this->view('test/create',$data);
4670
}
4771
}
48-
public function hola(Request $request){
49-
$model = new Test();
72+
73+
/**
74+
* @throws NotFound
75+
*/
76+
public function edit(Request $request){
5077
if ($request->isGet()){
51-
echo '<pre>';
52-
var_dump($request->getBody(), $request->getRouteParam('id'));
53-
echo '</pre>';
78+
$data =[
79+
'data'=>$this->model->getById($request->getRouteParam('id'),['password']),
80+
'model'=>$this->model,
81+
'metadata'=>[
82+
'title'=>'Hello page',
83+
'description'=>'Here is my page description',
84+
'keywords'=>['test','hello'],
85+
'author'=>'ishaf',
86+
]
87+
];
88+
if ($this->model->getById($request->getRouteParam('id'),['password'])){
89+
$this->view('test/edit',$data);
90+
}else{
91+
throw $this->exception;
92+
}
93+
94+
}
95+
}
96+
public function delete(Request $request){
97+
if ($request->isPost()) {
98+
$this->model->delete($request->getRouteParam('id'));
99+
App::$app->flashMessage->setFlash('success','Deleted '.$request->getRouteParam('id'),'/test');
100+
}
101+
}
102+
public function update(Request $request){
103+
if ($request->isPost()){
104+
$this->model->loaddata($request->getBody());
105+
if ($this->model->updateUser($request->getRouteParam('id'))){
106+
App::$app->flashMessage->setFlash('success','User Updated','/test');
107+
}
108+
else{
109+
App::$app->flashMessage->setFlash('success','cant Updated','/test');
110+
}
111+
$data =[
112+
'model'=>$this->model,
113+
];
114+
$this->view('test/create',$data);
115+
}
116+
}
117+
118+
/**
119+
* @throws NotFound
120+
*/
121+
public function show(Request $request){
122+
if ($request->isGet()){
123+
$data =[
124+
'data'=>$this->model->getById($request->getRouteParam('id'),['password']),
125+
'model'=>$this->model,
126+
'metadata'=>[
127+
'title'=>'Show',
128+
'description'=>'Here is my page description',
129+
'keywords'=>['test','hello'],
130+
'author'=>'ishaf',
131+
]
132+
];
133+
if ($this->model->getById($request->getRouteParam('id'))){
134+
$this->view('test/show',$data);
135+
}else{
136+
throw $this->exception;
137+
}
54138
}
55139
}
56140
}

app/core/App.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
1-
<?php
2-
3-
namespace app\core;
4-
class App
5-
{
6-
7-
/**
8-
* @var App
9-
*/
10-
public static App $app;
11-
public Router $router;
12-
public Request $request;
13-
public Controller $controller;
14-
public string $action = '';
15-
/**
16-
* @var View
17-
*/
18-
public $view;
19-
20-
public function __construct()
21-
{
22-
self::$app = $this;
23-
$this->request =new Request();
24-
$this->router = new Router($this->request);
25-
$this->view =new View();
26-
}
27-
28-
public function run()
29-
{
30-
try {
31-
$this->router->resolve();
32-
}
33-
catch (\Exception $e ){
34-
$this->view->render('_error',[
35-
"err"=>$e
36-
],$e->getCode());
37-
}
38-
}
1+
<?php
2+
3+
namespace app\core;
4+
use app\core\helpers\FlashMessage;
5+
6+
class App
7+
{
8+
9+
/**
10+
* @var App
11+
*/
12+
public static App $app;
13+
public Router $router;
14+
public Request $request;
15+
public FlashMessage $flashMessage;
16+
public Controller $controller;
17+
public string $action = '';
18+
/**
19+
* @var View
20+
*/
21+
public $view;
22+
23+
public function __construct()
24+
{
25+
self::$app = $this;
26+
$this->request =new Request();
27+
$this->flashMessage = new FlashMessage();
28+
$this->router = new Router($this->request);
29+
$this->view =new View();
30+
}
31+
32+
public function run()
33+
{
34+
try {
35+
$this->router->resolve();
36+
}
37+
catch (\Exception $e ){
38+
$this->view->render('_error',[
39+
"err"=>$e
40+
],$e->getCode());
41+
}
42+
}
3943
}

0 commit comments

Comments
 (0)