Route System



Routes appear as structures that allow you to manipulate the URI without changing the actual controller/method information. In ZN Framework, since the URI contains controller/method information, it can be rearranged in a more meaningful way and the way users want to see it.

Configuration

Route configurations can be made from 2 locations;

File: Config/Routing.php
File: Routes/*.php

 

 

# Route Configurations


You can also define route operations on the changeUri array in the route configuration file.

File: Config/Routing.php
'changeUri' =>
[
    '^product-list' => 'product/list',
]

The string key in the above array is the new route information. The value is what the new route information will actually run. Key values must be written in regular expression format, and you do not need to add a regular expression separator such as / at the end of the expression.

Apart from the route configuration above, you can perform more functional route operations using Routes/ directory.

 

 

# Methods


Route::uri(string $path = NULL) : void
Route::change(string $route) : this
Route::usable(bool $usable = true) : this
Route::csrf(string $type = 'post') : this
Route::method(string ...$methods) : this
Route::restore(mixed $ips, string $uri = NULL) : this
Route::ajax() : this
Route::redirect(string $redirect) : this
Route::show404(string $controller) : void
Route::direct() : this
Route::container(callable $callback) : void
Route::callback(callable $callback) : this

 

 

# URI [4.3.2]


Which controller/method the route will be made to. It is the final method used to complete the routing process.

Parameters

string $path It is specified to which controller and method the route will be made.

Usage

File: Projects/Any/Routes/Routes.php
Route::change('product-add')->uri('product/add');
Route::change('about-me')->uri('Home/about');

If you pay attention to the above, only controller and function information is written to the first parameter.

Default Function

If the parameter to be written does not contain function information, it is accepted as main.

Route::change('products')->uri('product');

The example route above runs the product/main controller when a request is made to localhost/products from the address bar.

Representational Expressions

In some cases, representational expressions can be used instead of directly writing a controller/function information. In other words, it may be possible for the controller or the function to be any statement. In such uses, the $x statement in regular expressions is used.

Route::change('({word})-({word})')->uri('$1/$2');

When the controller-method information is written to the URL above, a usage that redirects to the controller/method is shown. You can place the special expressions that you will write in the change() method wherever you want in the uri() method thanks to the $x statement.

 

 

# Change [4.3.2]


It is used to generate new path information to replace the target controller and function.

Parameters

string $route = NULL The new route.
return this

Usage

File: Routes/Routes.php
Route::change('product-add')->uri('product/add');

Based on the example above, you can now navigate to the controller for /product-add via the URL.

Warning: This usage does not provide an exact match. To do this, go to "Use of Regular Expressions".
Expression and Type Hinting

You can specify the type of data to be sent over the URL. If data other than this specified type is sent, the route will fail. The keys that can be used for these impositions are described below.

Route::change('product-add/id/:numeric')->uri('product/add');

In the example above, we created an expression and type imposition. Our new route must contain an id and a numeric value.

Available Keys

These keys are designed to send certain types of data over the URL.

:numeric Numeric data only.
:alpha Alphabetical data only.
:alnum Both numeric and alphabetical data.
:seo Data suitable for SEO. Example: this is an appropriate-for-seo-statement
:all All datas.
Use of Regular Expressions

To use regular expressions in the route, you can use the character sets in the list of special conversions given in the link below.

Regular expressions click for a list of custom transform.

Route::change('products/{word}')->uri('products/type');

In the above route, when products/any_word is typed in the URL, it will be redirected to the products/type controller.

You can also use the above example in a matching way.

Route::change('{starting}products/{word}{ending}')->uri('products/type');

Extending Regular Expressions (ZN >= 5.3.1)

You can extend the special expressions given above and create special uses for yourself. For this;

Configuration: Settings/Expressions.php -> regex

You can add the pattern you want to the regex array in the config file above.

'regex' =>
[
    '{phone}' => '[0-9]{10}'
]

After the definition above, you can use it in the route.

Route::change('profile-({phone})')->uri('user/profile');
Database Routing [5.3.3]

this method allows the data from the URI to be placed in the route by controlling it from the database. In other words, you can route according to the value coming from the database.

Usage: [tableName:columnName]

An example is given for a better understanding of its usage.

Route::change('[blog:slug]')->uri('blog'); 

The above usage URI checks the value from section 1 from the slug column of the blog table. If the result is found, it runs the blog/main method.

Route::change('profile/[account:name]')->uri('account/profile');

The above usage URI checks the value from section 2 from the name column of the account table. If the result is found, it runs the account/profile method.

One-Dimensional Data Decoding

If the data stored in the database is one-dimensional data of JSON, Serial or Separator type, the data must be decoded for this match. Usually used for multilingual decoding.

Usage: [tableName:columnName, decoderName:key]
decoderName json represents any of the class names serial or separator.
key If this part is not used, it uses the active language abbreviation en, such as tr. The string key that specifies which of the encoded data array to call.

Available Sample Data

+----+----------------------------------+------+
| ID | SLUG                             | BLOG |     
+----+----------------------------------+------+
| 1  | {"en":"what-is-zn","tr":"zn-nedir"}     |
| 2  | {"en":"installation","tr":"kurulum"}    |
+----+-----------------------------------------+

Default

The default value is the system's default language. Example: tr, en

Route::change('[blog:slug, json]')->uri('blog'); 

Automatic Language Change

If the extracted data is multi-language data consisting of language abbreviations, the selected language automatically changes according to the abbreviation to which the data entered through the URL belongs.

URL: /what-is-zn => Selected EN

URL: /zn-nedir   => Selected TR

Specifying a Value

Which of the parsed data to call.

Route::change('[blog:slug, json:en]')->uri('blog'); 

You can specify the value dynamically.

Route::change('[blog:slug, json:' . Lang::get() . ']')->uri('blog'); 
Displacement of Sections

You can swap the controllers, methods and parameters on the URL. The special pattern to be written for this is enclosed in parentheses(expressions), and these parentheses represent the $x usages. This is a feature that is also used in regular expressions.

In the example below, we want to edit our URL with the id between the account controller and its methods.

Route::change('account/({id})/({word})')->uri('account/$2');

The $2 URI in the above uri represents the value to be written to the 3rd section, ie ({word}).

$x Rule

this rule is the use of regular expressions itself. This expression is used in regular expressions to use an expression whose values are captured according to certain definitions. Here x denotes the number. The number of (expression) expressions used in regular expressions takes the value of x.

Route::change('account/({id})/({word})')->uri('account/$2');

The above example is denoted by ({id}) $1 and ({word}) $2 in the route.

 

 

# Usable [4.3.2]


After the route operation, you can block the entry through the original controller/method.

Parameters

bool $usable = true
Access via original controller/method? If set to False, access is denied.
return this

Usage

File: Projects/Any/Routes/Routes.php
Route::change('product-add')->usable(false)->uri('product/add');

With the above statement, access to the page will no longer be possible via product/add.

 

 

# CSRF [4.3.2]


It is used to check token against csrf attacks in the specified route. Use Form::csrf() must be used when you use the Form on the route you control this. If you are using regular HTML Form, the CSRFInput() method is used.

Parameters

string $type = 'post' post - get Form submission type.
return this

Usage

File: Routes/Routes.php
Route::csrf()->method('post')->uri('product/add');
Form Object

Form::csrf() method.

File: Project/Any/Views/product.wizard.php
@Form::csrf()->action('product/add')->open('formName')
    .
    .
    .
@Form::close()
CSRFInput Object

If you are not using a form class, it is the function corresponding to the same function.

File: Project/Any/Views/product.wizard.php
@Form::action('product/add')->open()
    @CSRFInput()
    .
    .
    .
@Form::close()

 

 

# Method [4.3.2]


It is used to determine which data types the requested page will accept. Usually, GET and POST requests are made to pages.

Parameters

string ...$methods What method or methods will be accepted.
return this  

Usage

File: Routes/Routes.php
Route::method('post', 'get')->uri('product/add');
GET

The request must be made through the URL. It blocks different types of incoming requests.

Route::method('get')->uri('product/add');
POST

The request must be made with the post method of a form object. It blocks different types of incoming requests.

Route::method('post')->uri('product/add');
Multiple

In order for more than one type of request to be accepted, it is sufficient to specify the types of requests to be accepted into the method() method sequentially.

Route::method('post', 'get', 'put', 'delete')->uri('product/add');

 

 

# Restore [4.3.2]


It can be used when working on the requested page. You can use this method to avoid possible code errors or trial results for users who make a request to that page at that moment, especially when you start working on that page in order to eliminate the error encountered on a page in a live environment. What you need to do here is to define the IP address (s) to access the page to be worked on. This configuration will prevent other users from accessing that page.

Parameters

mixed ...$ips Which IP addresses can log in.
string $redirect = NULL Which page other than the specified IP will be redirected to.
return this  

Usage

File: Routes/Routes.php
Route::restore('127.0.0.1', 'Home/restore')->uri('product/add');
Multiple IP

To allow multiple IPs at the same time, parameter 1 must be used as an array.

Route::restore(['127.0.0.1', '127.0.0.2'], 'Home/restore')->uri('product/add');

 

 

# Ajax (ZN >= 4.3.4)


It prevents access via URL to the page where you perform Ajax operations. That is, it makes the page available for AJAX only. This prevents your ajax pages from being manipulated through the URL.

Parameters

return this

Usage

File: Routes/Routes.php
Route::ajax()->redirect('Home/invalidRequest')->uri('product/ajaxAddItem');

 

 

# Redirect [4.3.2]


You can set where the routing will be done in case of an invalid route request.

Parameters

string $redirect It is used to set which controller/method to forward.
return this

Usage

File: Routes/Routes.php
Route::ajax()->redirect('Home/invalidRequest')->uri('product/ajaxAddItem');

If a job is made to the above product/ajaxAddItem address other than ajax, it will be redirected to the Home/invalidRequest address.

 

 

# Show404 (ZN >= 5.1.0)


this setting, which can be made through the Configuration file, can now be done via Routes/. Used to redirect invalid URL requests.

Parameters

string $controller Specifies which controller which method will open as 404.
return void

Optional Methods

6.84.4 direct()

Usage

File: Routes/Routes.php
Route::change('404')->show404('Home/s404');

In the above usage, we redirected to the Home/s404 controller after an invalid request. Since we also give a route to Home/s404, the directed page will be the /404 page.

Info: In this method, if the parameter is not specified with Route::change(), the default value is accepted as 404.
# Direct [6.1.0]

With this method, the controller and the function can be operated without directing, if desired.

Route::direct()->show404('Home/s404');

With the above use, when a request is sent to the invalid page, it will directly execute the Home::s404() method.

 

# Container [4.3.2]


It has been developed to allow the use of common rules. This method is used if more than one route will contain similar rules.

Parameters

callable $callback The routes to run.
return this  

Usage

File: Routes/Routes.php
Route::method('post')->csrf()->container(function()
{
    Route::uri('product/add');
    Route::change('product-list')->uri('product/list');
});

Route::method('get')->container(function()
{
    Route::uri('contact');
    Route::uri('contact/form');
});
Recursive [6.1.0]

With this update, the recursion feature has been added. Now the Route::container() method can be used inside. There was no recursion feature in previous versions.

Route::redirect('404')->container(function()
{
    Route::method('get')->container(function()
    {
        Route::ajax()->container(function()
        {
            Route::uri('product/edit');
        });
    });

    Route::method('post')->container(function()
    {
        Route::csrf()->container(function()
        {
            Route::uri('product/add');
            Route::uri('store/add');
        });
    });
});

 

 

# Callback (ZN >= 5.4.3)


It is used to run commands within the route.

Parameters

callable $callback The function to run.
return this

Usage

File: Routes/Routes.php
Route::change('user-{word}')->callback(function()
{
    Post::send('Example Data');

})->uri('user/profile');