- 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;
- postController
- indexAction: List all posts
- viewAction: View a selected post
- editAction: Edit the selected post
- authController
- indexAction: Login page
- loginAction: Same with indexAction (just call $this->indexAction(); from this action
- logoutAction: Sign out action
- 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;
- postController
- http://examplesite.com/post/ Because there is an indexAction
- http://examplesite.com/post/index
- http://examplesite.com/post/view/ID
- http://examplesite.com/post/edit/ID
- authController
- http://examplesite.com/auth
- http://examplesite.com/auth/index
- http://examplesite.com/auth/login
- http://examplesite.com/auth/logout
- http://examplesite.com/auth/register/{PARAMETER}
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. |
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.
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'. |