Nicotine

Open source framework for PHP & MySQL

Downloads Page

About $this->proxy available both in the controller and model

In order to prevent accidentaly overwritting members, many framework features are encapsulated into this object. Too many class members inherited from core, can lead problems. Nicotine is trying to minimalize user direct interaction with core members. This instance contains the following properties and methods:

<?php
// Call the getSomeBarFromDB() method form the /workspace/site/models/FooModel.php
$result $this->proxy->site->model('FooModel')->getSomeBarFromDB();

// Call the countFoo() method form the /workspace/admin/models/BarModel.php
$countFoo $this->proxy->admin->model('BarModel')->countFoo();

Notice that the filename should be same as model name. All models are recommended to be suffixed with Model word, in order to prevent naming collisions with controllers. Controllers should not be suffixed with Controller since their name will appear in URL. We will discus about this latter.

The back() method. Note that array argument are optional. This will redirect the guy back, one page, with optional messages. Useful on validation forms, so on.

<?php
// First argument: an array of messages to be displayed when the script comes back one page. Array keys are optional, but very useful
// when you need to display each message next to the filed. In this example, an input and a textarea.
$this->proxy->back([
    
'input_name' => 'Please provide the name of the user'
    
'textarea_address' => 'Please fill your addess'
]
// Second argument: string $messageType = "error" (default) | "warning" | "success"; Or you can set it's value to something custom.
'warning');

Front usage of the implementation:

<?php 
if (!empty($this->session('custom_errors'))) {
    foreach (
$this->session('custom_errors') as $error) { 
    
?>
        <p class="<?php print $this->session('messages_type'); ?>">
            <?php print $error?>
        </p>
    <?php 
    

}

Notice p class, in this example the value will be ="warning". Set it in your css file to something like .warning { color: orange; }. Dont't worry about session indexes created, they will be destroyed at the end of the script.

_GET

$this->proxy->get() Without arguments it will return the $_GET global variable.

$this->proxy->get('some_var') It will return $_GET['some_var']. There is no need to check if the some_var it is present on the super global variable $_GET. null will be returned in this case.

$this->proxy->get('some_var', 'default') It will return 'default' if 'some_var' is empty.

_POST

$this->proxy->post() Without arguments it will return the $_POST global variable.

$this->proxy->post('some_var') It will return $_POST['some_var']. There is no need to check if the some_var it is present on the super global variable $_POST. null will be returned in this case.

$this->proxy->post('some_var', 'default') It will return 'default' if 'some_var' is empty.

_REQUEST

$this->proxy->request() Without arguments it will return the $_REQUEST global variable.

$this->proxy->request('some_var') It will return $_REQUEST['some_var']. There is no need to check if the some_var it is present on the super global variable $_REQUEST. null will be returned in this case.

$this->proxy->request('some_var', 'default') It will return 'default' if 'some_var' is empty.

_COOKIE

$this->proxy->cookie() Without arguments it will return the $_COOKIE global variable.

$this->proxy->cookie('some_var') It will return $_COOKIE['some_var']. There is no need to check if the some_var it is present on the super global variable $_COOKIE. null will be returned in this case.

$this->proxy->cookie('some_var', 'default') It will return 'default' if 'some_var' is empty.

_SERVER

$this->proxy->server() Without arguments it will return the $_SERVER global variable.

$this->proxy->server('some_var') It will return $_SERVER['some_var']. There is no need to check if the some_var it is present on the super global variable $_SERVER. null will be returned in this case.

$this->proxy->server('some_var', 'default') It will return 'default' if 'some_var' is empty.

_SESSION

$this->proxy->session() Without arguments it will return the $_SESSION global variable.

$this->proxy->session('foo_key') it will return $_SESSION['foo_key'] if 'foo_key' it is present into session, null otherwise.

$this->proxy->session(['foo_key' => 'foo_value', 'bar_key' => 'bar_value']) it will store the array argument into session, as key => value pair.

Views

$this->proxy->view('optional_folder/file_name', compact('var1', 'varN')); from the controller. Load view associated with the controller. Folders can be nested. File name should be without .php extension. var1...varN are available vars into the view. They are optional.

View variables can be accessed in the view using: $this->vars->var1...$this->vars->varN where var1...varN are the variable names set in the controller.

$this->contentForLayout contains the rendered view. You should print this variable into layout.

Layout

In the controller, before declaring the view, set the layout, e.g. $this->proxy->layout = 'staff/roles'; where staff is a parent folder in this example, and roles is the name of the file. No need to declare the file extension, framework will assume that is .php.

Layouts are HTML files which contains a view. Many pages can have the same layout (same structure) but different content. A layout can contain for example <header>, <footer> and <sidebar> which are the same across the pages, whereas the view are the <main>, which is different, across the pages. Of course, you can have different or multiple layouts, for example Admin side layout, Site side layout, so on.

Checks

$this->proxy->isSiteRequest(): bool check if is a Site side HTTP request.

$this->proxy->isAdminRequest(): bool check if is an Admin side HTTP request.

$this->proxy->isAjaxRequest(): bool check if is an AJAX request.

$this->proxy->isCliRequest(): bool check if is an CLI request.

$this->proxy->isApiRequest(): bool check if is an API request. An API URL request begins with "/api/"

$this->proxy->isCronRequest(): string|bool check if is a cron request and return the cron name, or false on failure.

$this->proxy->isScriptRequest(): string|bool check if is a CLI script request and return the script name, or false on failure.