2.1 MvcAbstractController
MvcAbstractController¶
During a system development is very common you need to realize the same task in a several different places, for example, check out if there was a POST
. This is one of the reasons to MvcAbstractController
must to exist, with it we can provide you a lot of methods that are useful like short cuts for this kind of repeated tasks in your system.
By this way, you can implement your own methods here just extending this class and pointing the controller to this your new that we recommand to call AbstractController. [[You can found more details here.|2. Controllers and Views#abstractcontroller]]
But unless it's static, these methods can't be accessed from views or models, actually it can't be accessed from anywhere outside the scope from controller, so for do it you must to create a file functions.php
on the root of your project and implement your own functions that will be accessible from everywhere. That way you just have to do one include
to this file in your index.php
([[like this|1. Install#indexphp]]).
SuitUp Workflow¶
There's no secrets, SuitUp knows your controller extends the class MvcAbstractController, this is required. Some methods from this class are needed to the perfect work of framework and still in a correct order. By this way it's possible to create some rotines in your system to specifics situations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
preDispatch
and posDispatch
don't forget to call the original method too, otherwise SuitUp won't work properly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
What are default methods from SuitUp¶
The list below describes all of the methods which are default from MvcAbstractController and can be used inside everyone controller from your system.
$this->preDispatch();
¶
Launched before anything on the controller, executes internally some actions to the properly work of SuitUp. Must not to be override without to be called internally with parent::preDispatch();
.
$this->init();
¶
Launched after preDispatch()
, but before current action. For now, this method do not execute any internal action on SuitUp, so you don't need to call it with parent::init();
when override, even being a good practice to prevent future conflicts with missmatch versions. :eyes:
$this->indexAction();
¶
The index
action is called when the name of current action is not specified on URL ex.: http://yoursite.dev/
which will call the module default
, index
controller and index
action. This method was included on MvcAbstractController
to prevent when a new controller do not implement this action. When overridden do not need to call parent::indexAction();
.
$this->errorAction();
¶
Always that SuitUp found on the way one Exception
not treated, it dispatchs to the error window of [[ErrorController|2.2 ErrorController (en)]]. In this controller named ErrorController
will be executed this action if it won't of the type 404 (Page not found).
Read more in: [[ErrorController|2.2 ErrorController (en)]]
$this->notFoundAction();
¶
Always that SuitUp found on the way one Exception
not treated, it dispatchs to the error window of [[ErrorController|2.2 ErrorController (en)]]. In this controller named ErrorController
will be executed this action when SuitUp is not able to found module, controller, action or view file.
Read more in: [[ErrorController|2.2 ErrorController (en)]]
$this->posDispatch();
¶
Launched after any controller action, executes internally some actions to the properly work of SuitUp. It must not to be overridden without to be called internally with parent::posDispatch();
.
$this->getMsgNsp();
¶
We don't wanna mixes messages dispatched in different modules on your system, so this method returns the namespace
for the current module. But what about this messages?
A little while below you'll realize that exists a method $this->addMsg($msg, $type = MsgType::INFO, $withRedirect = false);
, with parameter $withRedirect = true
SuitUp will retain this message on the $_SESSION
to show it only in the next page, normally used before a $this->redirect($to);
.
$this->getModuleName();
¶
Return the current module name.
Sample:
Module default (http://seusite.dev/
), return: default
Module Admin
(http://seusite.dev/admin
), return: admin
$this->getControllerName();
¶
Return the current controller name.
Sample:
http://seusite.dev/
: Return index
http://seusite.dev/admin/user
: Return user
$this->getActionName();
¶
Return the current action name.
Sample:
http://seusite.dev/
: Return index
http://seusite.dev/admin/user/edit
: Return edit
$this->getLayoutName();
¶
Return the layout name which is being used at the moment. [[Click here|3. Layouts (en)]] to understand better about what is a layout
for SuitUp.
$this->setLayout($name, $path = null);
¶
1 2 |
|
Is possible to change the layout in execution time, see the example below:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
$this->renderView($renderViewName, $vars = array(), $renderViewPath = null);
¶
1 2 3 |
|
Render a view is a therm that means you wish capture a HTML file and it's contents, you can even inject variables on it. It's very commom when, for example you need to send an e-mail through HTML to your client with some data as an ordered.
$this->addViewVar($name, $value = null);
¶
1 2 |
|
After to do the actions you need in your controller you probably will need transfer some data to the view, this method make exactaly it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
$this->isViewVar($name);
¶
1 |
|
You can check if a variable already was transferred to the view using this method.
$this->getViewVar($name);
¶
1 |
|
Even before view dispatch you can get the value from a variable using this method.
$this->getParams();
¶
This method return all parameters indicated through URL (query string GET) with parameters defined on the [[routes|6. Routes]].
$this->getParam($name, $default = null);
¶
1 2 |
|
This method will return the parameter (URL or Route param) with the name indicated by $name
parameter from method and $default otherwise.
$this->isPost();
¶
This method is extremely simple, but just equal important. With it is possible to check out if there was a POST from a form or WebService. Returns just true
or false
.
$this->getPost($name = null, $default = null);
¶
1 2 |
|
You can use this method to get all POST values or just a single one item just using or not the parameters.
~~$this->setLogin(array $data = array());
~~¶
Still not implemented. You can create the login to your system direct in the variable $_SESSION
as we'll show below.
The MvcAbstractController got the attribute $authNsp
which works like namespace and allows you to override it to you create a different session for each module from your system. That means that even being just only one system you can sign in with different users in different modules at the same time.
Simple Login¶
1 2 3 4 5 6 |
|
Login with namespace
by module¶
1 2 3 4 5 6 |
|
namespace
and with it done the login data for different modules will be independent each other. After that is enough follow previous step to save the login data on $_SESSION[parent::$authNsp]
.
$this->isLogged();
¶
Return true
if exists something in the current login namespace
and false
otherwise.
static $this->getLogin($key = null, $default = null)();
¶
1 2 |
|
If $key
parameter was not passed so this method will return all in this namespace
session. When $key
parameter exists this method will seek for this index return it, but if this index does not exists so this method will return $default
value.
Something interesting to be noted in this method is that it can be accessed from everywhere, even from the views statically.
Sample:
1 |
|
static $this->updateLoginKey($key, $value);
¶
1 2 |
|
Updates an index of the session namespace
to the module. If the index was not found this method does nothing.
Just like
getLogin
this method is static too and can be called from everywhere.
$this->addMsg($msg, $type = MsgType::INFO, $withRedirect = false);
¶
1 2 3 |
|
This method add a message to be dispatched separated by type by the system. To capture this message just at the next page just make true
third parameter $withRedirect
.
Sorry, we have to talk about Bootstrap. Nowadays several sites on web are made with Bootstrap and it brings alerts that are colorized boxes to show messages to the users. By default Bootstrap uses 4 colors to this boxes (alert-success, alert-warning, alert-info and alert-danger). Because of this if you open [[this file from SuitUp|https://github.com/braghimsistemas/suitup-php/blob/master/src/Enum/MsgType.php]] you will see exactaly these four types of messages. We really like Bootstrap, it's just awesome!
You can create your own types of messages with no problems, but if you will make it we recommand that it's to be done using constants to prevent typewrite mistakes and useless bugs. It's a very good practice.
To know how to get these messages from the view and layout [[click here|3. Layouts#mensagens-de-layout]].
$this->uploadFile($file, $where, $exitFilename = null, array $allowedExt = array('jpeg', 'jpg', 'pdf', 'png', 'gif'));
¶
1 2 3 4 |
|
This method make the upload of the files by HTML forms.
If you open
composer.json
file from SuitUp will see that there's arequire verot/class.upload.php
. We aren't trying to make SuitUp your only one way to do what you have to do with your application. Actually we stand up for the use of libraries which are easy to handle and make all the hard work so instead of make our own file upload module we recommand [[verot/class.upload.php|https://github.com/verot/class.upload.php]] which is open source and available for years doing even more than needed.
If you feel this method do not suply your need, feel free to override it on your AbstractController.
Warning.¶
This method trows an
Exception
in error cases, so don't forget to "cover" your code with atry, catch
$this->uploadFileImageBase64($file, $maxFilesize = 524288);
¶
1 2 |
|
It's possible to save an image not as a file, but as a kind of string
using for that base64
, you just need to use this method.
Warning.¶
This method trows an
Exception
in error cases, so don't forget to "cover" your code with atry, catch
$this->getReferer();
¶
This method returns the URL user was before came to the current page. It is made through $_SERVER['HTTP_REFERER']
.
$this->redirect($to);
¶
1 |
|
This method is simply a short cut to the header('Location: $to');
Use to call this method with a return, so the system will literaly stops after that.
1 2 3 4 |
|
$this->ajax(array $data);
¶
1 |
|
It's very common instead HTML there is the need from some action to return a JSON, being for an AJAX or even to build a WebService module. For that just use this method at the end of action passing an array
formated just like you want the JSON. This own method avoid any HTML show and dispatch the header("Content-Type: application/json; Charset=UTF-8")
too.
Warning!¶
Anyone change of code after this method has no effect, it just make a literaly
exit
which does application really stops after dispatch JSON result.
SessionFilter¶
You probably already needed to use data from a form to filter a result list. To do this you can use GET
which get parameters from URL or POST
which get it from another protocol and it's more safe.
Firstly we like to crimp that in most cases to use GET
it's a really a good idea because like that the filters can be shared, but there's cases which this can be dangerous or you just don't wanna make the URL ugly.
Whatever way, when you make filters by
POST
and use pagination at the same time the filter data will be lost after the first page, to prevent it we made the methods below which help you in a simple way to keep the filter data even with pagination case.
How it works?
Through an especific namespace
for each action from your system the SuitUp separate these filter data and store it in the session ($_SESSION
).
Functional workflow sample.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
$this->addSessionFilter($name, $value = null);
¶
1 2 |
|
Add or update one or more values to the items on the unique form filter data space for the current action.
$this->getSessionFilter();
¶
Return all the data to the current session filter.
$this->removeSessionFilter($key = null);
¶
1 |
|
Remove some index data from the current session filter.
$this->clearSessionFilter();
¶
Remove all the data to the current session filter.