Generate Url & Redirection
Required Namespace
  • Ness
    • Url

You can include this library to your code file by typing the below code;

use Ness\Url


About Url Class #top

Ness PHP uses controllers to map your routes. You does not require to specify routes when you are codding your page relationships. You can read URL Structure of Ness PHP before reading and trying redirections.


Redirect to Controller's Action #top

Let's say you are developing a blog application and you have two controllers a postController and authController with actions;

  1. postController
    1. indexAction: List all posts
    2. viewAction: View a selected post
    3. editAction: Edit the selected post

  2. authController
    1. indexAction: Login page
    2. loginAction: Same with indexAction (just call $this->indexAction(); from this action
    3. logoutAction: Sign out action
    4. registerAction: Create a new user account

For the structure above your controllers should look like;
               
    class postController extends Ness\Controller{
    
        public function indexAction(){
            //Your codes here
        }
    
        public function viewAction($id = 1){
            //Your codes here
        }
    
    
        public function editAction($id = 0){
            //your codes here
        }
    }
                            


            
    class authController extends Ness\Controller{
    
        public function indexAction(){
            //Your codes here
        }
    
        public function loginAction(){
            //Your codes here
        }
    
    
        public function logoutAction(){
            //your codes here
        }
    
        public function registerAction($type = 'admin'){
            if($type == 'admin'){
                //Admin registration
            }else {
                //Member Registration
            }
        }
    }
                            


For these two classes above generatable urls should be;
  1. postController
    1. http://examplesite.com/post/ Because there is an indexAction
    2. http://examplesite.com/post/index
    3. http://examplesite.com/post/view/ID
    4. http://examplesite.com/post/edit/ID

  2. authController
    1. http://examplesite.com/auth
    2. http://examplesite.com/auth/index
    3. http://examplesite.com/auth/login
    4. http://examplesite.com/auth/logout
    5. http://examplesite.com/auth/register/{PARAMETER}
Let's say you want to generate an url for post controller's viewAction and with post ID 4785;
                                Url::RedirectToAction("post", "view", "4785");
                            
This code line will return a string (which you can use it with html's a tag); http://examplesite.com/post/view/4785


Method Explained
                                Url::RedirectToAction($controller_name, $action_name, $parameter_if_available);
                            
This function returns a redirectable url from your controllers's actions. For example you can use this with html's a tags href attribute.
Return String
Parameters $controller_name String; Cotroller name with out 'Controller' suffix
$action_name String; Action name with out 'Action' suffix
$parameter_if_available String; If action method contains a parameter you can set a parameter here. [Note]: if actionnameAction does not contains a parameter you should not set this.

Recommendation Ness PHP recommends you to create always Actions with parameters. With default value Even if you won't use it.


Create & Divide Projects #top

Ness PHP supports creating areas by adding a sub-folder to 'Controller' directory. You can create new folders in Controller directory to divide big projects and get rid of the complexity.

Let's say you are developing a project with backend and you need to separate admin area from member area. Create a folder in Controller path called 'adminArea'.You can create now unlimited controllers in adminArea to manage your url's, codes and logic.

Area Suffix Do not forget to add Area suffix to your folders. Ness PHP looks for 'Area' suffix to undertand that folder is area and should make redirections to controllers in that folder.



Method Explained for Area Redirections
                                Url::RedirectToArea($area ,$controller_name, $action_name, $parameter_if_available);
                            
This function returns a redirectable url from your controllers's actions in an Area. For example you can use this with html's a tags href attribute.
Return String
Parameters $area String; Area name without 'Area' suffix $controller_name String; Cotroller name with out 'Controller' suffix
$action_name String; Action name with out 'Action' suffix
$parameter_if_available String; If action method contains a parameter you can set a parameter here. [Note]: if actionnameAction does not contains a parameter you should not set this.



Here is an example for creating a redirectable url in adminArea->userController->editAction->userid=4758
                                Url::RedirectToArea('admin', 'user', 'edit', '4758');
                            
This will return: http://mysite.com/user/edit/4758?p=admin


Other Methods Available #top

Url class contains some more methods which can be useful in your projects. Here is a short description about that commands.


getArea
                                    Url::getArea();
                                
Get area data from url.
Return String
Parameters None



Redirect
                                    Url::Redirect($url, $sec);
                                
Redirect directly.
Return Request
Parameters $url String; Url to get redirected
$sec Integer; seconds to wait before redirection



getUrl
                                    Url::getUrl();
                                
Get the current url
Return String
Parameters None



getData
                                    Url::getData(String $id);
                                
Get the data from parameter in url.
Return String
Parameters $id String; parameter to get data from. for example; www.mysite.com/users/edit/4758?p=admin&option=delete

if you call the Url::getData("option"); that will return 'delete'.