Controllers



Controllers are things that are used to manage view pages and are usually designed with Object-Oriented Programming. Users' URL requests are made to the controllers and to the methods of those controllers. The PHP code that needs to work is included in the methods in which this request is made. In other words, PHP code is written into the controller in object-oriented programming logic. It is also the code written to the controllers that allows HTML-weighted pages to be called and displayed to the user. The architecture that separates PHP , HTML, and SQL code called MVC is used in the ZN code frame that needs to be described here . It is therefore the controllers that control this architecture. The controllers are in the following sequence.

Index: Projects / Frontend (ProjectDirectory) / Controllers /

We will give the URLexamples used in the narration localhostassuming you are working on them.

 

# Section Headings


# ZNA and the MVC
# Simple Code to Run
# 1 Section Controller

# 2 Chapter controler Method
# 3 Part Parameters
# Default Landing Pad
# Route Controller for

 

# ZN and MVC


In the ZN Framework, URL requests are made to the Controller. Controllers also invoke views with HTML content. Thus, the controller will bring the page that is encountered after the request. In this process, database operations are also performed in the models. These models are also used by controllers. In general, the logic is as follows.

Controllers It mainly includes PHP code.
models Includes Database commands.
Views Contains predominantly HTML code

 

# Running a Simple Code


Below is a sample code that gives the message "Welcome to World of ZN". blog.phpCreate a file with the same name as the directory below and edit the contents as follows.

File: Projects / Frontend (ProjectDirectory) /Controllers/blog.php
<?php namespace Project\Controllers;

class Blog extends Controller
{
    public function main()
    {
        echo "Welcome to World of ZN";
    }
}

The URL that should be entered in the address bar to run this code is below.

Run: localhost / blog / main or localhost / blog

 

# 1 Section Controller


The controller should be placed in the Projects / Any / Controllers / directory. The current predefined controller of ZN is home.php . And in this controller is the  main method. Requests made via the URL do not need to be used in the address bar of the main address.home.phpTo run the main method contained in the address bar localhost / home is enough.

The controller's file name must match the class name . The controller name specified on the URL represents the file name of that controller, not the class name. For this reason the URL is written in blogs, not blogs.

File: Projects / Frontend / Controllers / blog.php
<?php namespace Project\Controllers;

class Blog extends Controller
{
    public function main()
    {
        echo 'Home Page';
    }
}
Run: localhost / blog

 

# 2 Partition Controller Method


In the address bar, the first name of the controller to be accessed is the file name, and the second is the name of the method to be executed by that controller. If the method name is main , it does not need to be specified on the URL.

File: Projects / Frontend / Controllers / blog.php
<?php namespace Project\Controllers;

class Blog extends Controller
{
    public function main()
    {
        echo 'Welcome to World of ZN';
    }

    public function comments()
    {
        echo 'Run Now!';
    }
}

To commentsrun the above method, we edit our URL address as follows.

Run: localhost / blog / comments

To run the Main method, you simply type the name of the controller in the URL address.

Run: localhost / blog

 

# 3 Section Parameters


The third and subsequent sections of the URL address are used to send the parameters to the methods via the URL . If the main method is used to send parameters , the second and subsequent sections are used as parameters. The parametric values ​​sent over the URL hold the parameters of the corresponding method. The following example shows how the parameters can be used.

File: Projects / Frontend / Controllers / products.php
<?php namespace Project\Controllers;

use Output;

class Products extends Controller
{
    public function main($price, $value)
    {
        Output::write('{0} - {1}', [$price, $value]);
    }

    public function computer($price, $value)
    {
        Output::write('{0} - {1}', [$price, $value]);
    }
}
Run: localhost / products / computer / price / 1000
price - 1000

You do not need to use the method name if you want to make a request to the Main method.

Run: localhost / products / price / 5000
price - 5000

 

# Default Opening Controller


The home controller is the home.php file , which is set by default in the ZN code frame . You do not need to specify the opening controller on the URL. For this reason, this controller is working directly when the URL address is localhost / . To change this, you need to open the Config / Services.php file and change the following route setting.

File: Projects / Frontend / Config / Services.php -> route -> openController
'openController' => 'home'

 

# Route Controllers ( ZN 3 - ZN 4 - ZN FE )


You can take advantage of route controllers for non-compliant or flexible namespaces with the ZN Frameworkte OOP.

Use of

File: Projects / Frontend / Controllers / users.php
Route::run('get-list', function()
{
    Import::view('get-list');
});
Run: localhost / users / get-list
Route::run('main', function()
{
    Import::view('main-view');
});
Run: localhost / users