Ness PHP Resource Management
Required Namespace
  • Ness
    • Resources

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

use Ness\Resources


About Resource Management Class #top

Ness PHP Framework contains a resource class which is designed to help you to manage your static values. You can define two type of resource in the current version of Ness.

A string resource provides you an external file to get static string values which can be Application name, address lines, color codes or any other static data.

Image resources provides you an external xml file to store local or remote images with width and height attributes also base64 values, so you can include images to your html with a single line.


Enable Resources #top

Please follow this simple instructions to start using Resource helper class in your projects.

  1. Create a resource file in your project root or anywhere else in your project's application path. There is also a default empty resource file available called 'resources.xml'. Please do not forget you must define a valid xml file to define your resource values.
  2. Define a new string/image value your xml resource file.
  3. Include Resource class in your code file and start using resources.



Resource Types & Resource Definitions #top

Here is a short description & example for using Resource class and defining a new resource in your projects.


String Resources

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string id="appname">Empty Ness Application</string>
        <string id="version">1.0</string>
        <string id="author">Sinan SALIH</string>
        <string id="message1">Good Morning User</string>
    </resources>
                            



Image Resources

    
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <image id="myimge" width="48" height="48">C:\Users\Images\image1.png</image>
        <image id="myimge2" width="48">data:image/jpeg;base64,/9j/4AAQSkZJ........</image>
        <image id="myimge3">https://www.site.com/images/image.jpg</image>
    
        <string id="appname">Empty Ness Application</string>
        <string id="version">1.0</string>
        <string id="author">Sinan SALIH</string>
        <string id="message1">Good Morning User</string>
    </resources>
                            



Methods #top

Get String
Resources::String->get(string $prm1);
Get a string value from xml
Return String
Parameters $prm1 String; id in xml file for string tag



Get Image
Resources::Image->get(string $prm1);
Get a Image with ready to use html img tag.
Return String / Image(img) tag for html views.
Parameters $prm1 String; id in xml file for image tag



Get Image Value
Resources::Image->getValue(string $prm1);
Get a value set in image tag. This method can be used for getting paths and a image object.
Return String
Parameters $prm1 String; id in xml file for image tag



A Simple Example for Resource Class #top

Here is a short example to show how to use your resources in php code.

Get String values from xml
Assume that we need to get a greeting message from the xml file. Lets create a xml resource file; Create the following file:

[project_root]/Application/Resources.xml and define your values in the xml file;


    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string id="msg1">Hello Welcome to my new Ness PHP Project</string>
        <string id="author_name">Sinan SALIH </string>
        <string id="style_red_color"> style="color:red;" </string>
    </resources>
                            


Now we need to check app_config.php file if the Resource.xml is defined.

    /*
    * === [Application Resource Files ] ===
    * This function is used to set the
    * path of the resource.xml file which is used
    * to store static values for your projects(ex; strings, image paths etc)
    * Note; the core app location ('Application' by default) is excluded from the value.
    */
    Configuration::setResourceFile('Resources.xml');
                            
find the lines above and check if the filename of resource file is true.

Thats All. Now we can start using resources in our codes. Lets create an action in index controller to show the message from xml file;


    <?php
    
    use Ness\Controller as myController;
    use Ness\Resources;
    
    class indexController extends myController
    {
        public function indexAction($param = 0)
        {     
            echo Resources::String()->get("msg1")." by ". Resources::String()->get("author_name");
        }
    }                     
                            

Result:
Hello Welcome to my new Ness PHP Project by Sinan SALIH


Get Images & Image Values from xml
Follow the steps above to create a xml file and check the app_config.php file for the Resource file path.

Add image values to resource file;

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <image id="fw_logo">https://github.com/nessphp/framework/blob/master/Application/Content/img/ness_logo_rounded.png?raw=true</image>
        <image id="fw_logo_small" width="48" height="48">https://github.com/nessphp/framework/blob/master/Application/Content/img/ness_logo_rounded.png?raw=true</image>    
    </resources>
                            


Assume that we have a view called startpage.php and want to show the image set above;


        <?php
        use Ness\Resources;
    
        $page = new Ness\UI\Page();
        $page->setLayout('master_page.php');
        $page->BeginContent();
    ?>
    <!-- PAGE CONTENT STARTS -->
    
    
    <h5>Original Image Get From Xml</h5>
    <?= Resources::Image()->get("fw_logo");?>
    
    <hr>
    
    
    <h5>Get Image With Properties</h5>
    <?= Resources::Image()->get("fw_logo_small")?>
    
    
    <hr>
    <h5>Image Value Get From Xml</h5>
    <img src="<?= Resources::Image()->getValue("fw_logo");?>" height="48" width="48">
    
    
    
    
    <!-- PAGE CONTENT ENDS -->
    <?php
        $page->EndContent();
    ?>
                            


Result: