Usage of Startup Files



Your projects may have code blocks that you want to run before your code runs. It is used to create blocks of code that you want to run in such situations, that is, before your codes are switched on. Any function, fixed or different code blocks can be executed in these files. Our main goal is to enable users to intervene in the existing structure. The configuration for this section is in the following path.

File:  Config / Starting.php

 

 

# Section Headings


# Automatic Loading
# Manual Loading

# Methodical Use

# Layers ( ZN >= 5.3.6 )

# Top / Upper Layer
# Top Bottom / Middle Top Layer
# Middle Top / Middle Top Layer
# Middle / Middle Layer
# Middle Bottom / Middle Underlayer
# Bottom Top / Gold One Top Layer
# Bottom /

# Creating a Startup Controller

# Exclusion ( ZN> = 5.2.0 )
# Incorporation ( ZN> = 5.2.0 )
# Controller Data Transmission ( ZN> = 5.2.9 )

 

 

# Automatic Upload


You need to put the file that contains your codes that you want to run initially into the Starting / Autoload / directory. If these files apply to all your projects, you need to export them to the External / Starting / Autoload / directory. In order for these files to be automatically installed, the setting file autoload: recursive = true needs to be set. After this process, you can see that your file is automatically uploaded.

'autoload' =>
[
    'status'    => true,
    'recursive' => false
]

 

 

# Manual Loading


If you want to load the file that contains the code you want to run initially into the Starting / Handload / directory, you need to specify the file name in the handload setting in the configuration file .

'handload' => ['file1', 'file2']
Methodical Use

You can also include these types of functions with the above method Import :: HandLoad ( 'file1' , 'file2' , ..., 'fileN' ) .

File:  Controllers / ExampleController.php
<?php namespace Project\Controllers;

class ExampleController extends Controller
{
    public function main()
    {
        Import::handload('example-file');
    }
}

 

 

# Layers ( ZN >= 5.3.6 )


Layers are designed to allow you to integrate code into every corner of the ZN Framework. The difference is that this usage is not constant from using startup files. In other words, the codes you write work in the layers they are in. These files are located in the following directory.

layers

Starting / Layers /

Top.php
TopBottom.php
MiddleTop.php
Middle.php
MiddleBottom.php
BottomTop.php
Bottom.php

Top / Top Layer

The code to be written to this layer runs before the system files are loaded. For this reason, you can not use ZN libraries.

Earlier Works

The structures that worked before this layer are below.

System Constants
  Index Constants
  Space Character constants

Top Bottom / Superior Bottom Layer

You can use system constants and libraries in this layer since the code to write to this layer is used immediately after the auto loader. All Config files can be configured on this layer since this layer runs immediately after the auto installer.

Earlier Works

The structures that worked before this layer are below.

  Top Layers Construction
  Autoclave

Middle Top / Middle Top Layer

The codes to be written to this layer will run just before the kernel comes into play. ButHtaccess, Autoloder and Header It enters the circuit after configuration.

Earlier Works

The structures that worked before this layer are below.

Structure of Top Layer
Directory URL Constants
  Active Controller Constants
  Built-in functions
  Class of Aliases
Project Configurations
.htaccess cache: obgzhand and Configuration

Middle / Middle Layer

This layer works only after the initialization codes of the core have been switched on. Attach additional code to this layerRoutes, Vendor Downloads, Startup Files Activated.

Earlier Works

The structures that worked before this layer are below.

Top LayersConfiguration
  Htaccess Configurations
  Robots Configurations
FileSystem Configurations
Autoloader Configurations
Initial Files
  Restoration Configurations
Route Configurations

Middle Bottom / Middle Bottom

This layer comes into play after your core works. The codes in the other layer will run before this layer. This layer only enters the kernel immediately before the end codes.

Earlier Works

The structures that worked before this layer are below.

Top Layers Configuration
  Loading Controllers
  Automatic View Loads

# Bottom Top / Gold One Upper Layer

In this layer all the processes, including the end codes of the nucleus, are executed. The codes to work immediately after the core are located on this layer.

Earlier Works

The structures that worked before this layer are below.

Top Layers Configuration
  End Controller

Bottom / Lower Layer

The layer that came in after the whole system. The codes to be executed after the system runs are written to this layer.

Earlier Works

The structures that worked before this layer are below.

Top Layers Structure
Logs
  Buffering

 

 

# Creating a Startup Controller


You may want to run the codes that apply to the whole system before all controllers. You can use one of your existing controllers to write code that will run throughout the system.

'controller' => 'StartingController:functionName'

If more than one controller or function connected to the same controller is to be run, you must specify it as a value sequence.

'controller' => ['StartingController1:functionName', 'StartingController2:functionName']

Here is an example usage.

File:  Controllers / StartingController.php
<?php namespace Project\Controllers;

class StartingController extends Controller
{
    public function main()
    {
        if( CURRENT_CFPATH !== 'user/login' && ! User::isLogin() )
        {
            redirect('user/login');
        }
    }
}
File:  Config / Starting.php
'controllers' => 'StartingController'

The function name main does not need to be specified.

Exclusion ( ZN >= 5.2.0 )

In some cases you may want some controllers not to have the initial control run. For example, when a login operation is performed, the login controller can be directed to control the login control in the startup controller. You should not exclude some controllers with the reason for such situations. The excludeconstant is used for this .

File:  Controllers / StartingController.php
<?php namespace Project\Controllers;

class StartingController extends Controller
{
    const exclude = ['user/login'];

    public function main()
    {
        if( User::isLogin() )
        {
            redirect('user/login');
        }
    }
}
Inclusion ( ZN> = 5.2.0 )

It's the opposite of excluding pages. This constant ( include) specifies on which controllers the initial control will be used when it is used. Both exclude and include constants can be used together if desired.

File:  Controllers / StartingController.php
<?php namespace Project\Controllers;

class StartingController extends Controller
{
    const exclude = ['user/login'];
    const include = ['home', 'product', 'contact'];

    public function main()
    {
        if( User::isLogin() )
        {
            redirect('user/login');
        }
    }
}
Data Sent to Controller ( ZN> = 5.3.9 )

The View class used to send the view data can send data to the currently working controller if requested. There are 2 methods for this .

Constructor defines const extract = true .
 Config / Starting.php -> extractViewData = true definition.

File: Controllers / StartingController.php
<?php namespace Project\Controllers;

class StartingController extends Controller
{
    public function main()
    {
        View::sendData('Send Data')->otherData('Other Data');
    }
}

If one of the 2 methods mentioned above is used after the above example data transmission,$ this These values ​​can be reached with the object.

File: Controllers / MyController.php
<?php namespace Project\Controllers;

class MyController extends Controller
{
    const extract = true;

    public function main()
    {
        echo $this->sendData;
        echo $this->otherData;
    }
}
Send Data
Other Data

If the submissions are valid for all controllers, it would be wise to activate it from Config / Starting.php instead of defining const extract on each controller .