22
33namespace app \controllers ;
44
5+ use app \core \App ;
56use app \core \Controller ;
7+ use app \core \Exceptions \NotFound ;
68use app \core \Request ;
7- use app \middlewares \authMiddleware ;
89use app \models \Test ;
910
1011class 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}
0 commit comments