path() have been removed while methods such as import()
and export() have been updated to effect this change. However, the new(),
dir() and xurl()
methods are not affected by this update. A new class Ress now handles static resource loading by default.
Resource class. The primary function of Resource
class is to load external static resources like javascript or css files. These files are stored in container groups where they can later be
imported into web pages. The following listed processes are handled by the Resource class.
Resource class provides useful static and non-static helper methods for managing static
resources. These helper methods provides assistance in storing local and remote files links into groups,
naming file links, merging file links or resource groups and importing stored group as resolved script links
into the web page. These methods are listed below:
res.
Therefore, all local static urls should begin with the res folder name although
a different folder name may be accepted. Before a file can be imported, it can
be initially stored into the Res storage system. By default, the Resource class stores
urls into different containers which are either named or unamed. A named container
uses a group name to store static urls. However, if a group name is not defined, then such urls will
be stored in an unamed space. There are
several methods used for handling resource storage and importation and they are listed below
<?php
Res::name('css');
name() method to declare a resource name.
The resource name declared is used as a unique storage space name for resource file urls which can
later be imported in the web application. In earlier versions, the Res::name()
does not support a chainable structure. However, starting from version 2.6.0, the Res::name()
returns the object instance of the class, making it possible to chain other methods. The sample below is how to use
the name() method as a chain in line 4 in the earlier versions.
1. Res::name('css'); 2. Res::name('js'); 3. 4. Res::new()->name('css')->name('js');
2.6.0, the name() method can be easily chained as shown below:
1. Res::name('css')->name('js');
Res::callFile() Res::getFile() and
Res::addFile(). Although, these methods are mostly used to set urls, they differ in their
functions and roles. Examples are displayed below:
Res::name('css'); //declare new resource storage group name
Res::callFile('res/css/file1.css'); //add url to css group
Res::callFile('res/css/file2.css'); //add url to css group
Res::name('js'); //declare new resource storage group name
Res::callFile('res/js/file1.js'); //add url to js group
Res::callFile('res/js/file2.js'); //add url to js group
css is where file1.css and file2.css
paths will be saved. While the file1.js and file2.js
will be saved into js storage group declared just immediately above them. These groups
can be imported later using the Res::import() method. Paths stored does not necessarily
have to be of the same extension. However, only four files extensions are supported which are
html css js and php. The callFile() method
does not only add urls but it also returns the generated script of any added url. This is shown below:
<?php
Res::name('css');
$script1 = Res::callFile('res/css/file1.css'); // add valid relative path of css file
$script2 = Res::callFile('https://www.foobaz.com/some/file1.js'); // add external file link of a javascript file
var_dump($script1); // <link href="current_domain/res/css/file1.css" rel="stylesheet" />
var_dump($script2); // <script src="https://www.foobaz.com/some/file1.js"><script/>
callFile() method will always return the relative script of any supported resource url. This is mostly detected through
the file extension. However, it is noteworthy to mention that relative urls as in $script1 above are usually validated
before the script is generated or it is being stored. Invalid relative urls of files are never accepted and will return an empty response.
This means that for relative urls, the file corresponding to such urls must exist within the specified relative path. Contrary to this, as in the
case of $script2, absolute urls starting with http:// or https:// protocols are never validated because they are assumed as external links.
The callFile() method is mostly difficult to maintain because it does not support a chainable structure. The equivalent of this method is the url() method
which returns the resource class object instance.
Res::name('css')
->url('res/css/file1.css') // add resource url to group
->url('res/css/file2.css') // add resource url to group
->name('js')
->url('res/css/file1.js') // add resource url to group
->url('res/css/file2.js'); // add resource url to group
url() method supports a chainable structure that
makes the code more organized and easier to read.
$resolved = Res::getFile('res/css/file2.js')
callFile() method, the getFile() method also resolves its urls
in a similar manner and returns the resolved url. The major difference is that getFile() automatically
imports the valid or external url directly to the webpage using the relative script tag.
Res::name('group1'); //declare new resource group1
Res::name('group2'); //declare new resource group2
Res::addFile('group1','res/css/file2.js'); //select group1, add new resource url
Res::addFile('res/css/file3.js'); //still select group1, add new resource url
addFile() method, is another method that is used to add urls only to an existing resource group.
The existing group name to which the url will be added is declared as the first argument
while the second argument defines the new url to be added. After the first group declaration,
addFile permits the omission of the class name for subsequent chained addFile
calls.
Res::open() is used to open a resource inclusion on safe mode while Res::close()
is used to close currently opened resource group or to move back into the unamed storage space.
# Method 1 ---------------------------------------------------- Res::open(); // safe opening Res::name('css') // declare new storage space name ->url('somefile.css'); // add a new resource url to storage space
Res class to store
resource urls before the Res::open() was called, by redeclaring the Res::open()
as in the file above, it will ensure that the current file is able to start on a clean slate without affecting
the previous resource urls. This ensures a smooth continuity in the new file. The method above is a sample of
how a resource file can be started on a safe mode in earlier versions. In version 2.6.0, the
Res::open() now returns the object of the resource class. Now we can easily chain other methods as shown below:
# Method 2 ---------------------------------------------------- Res::open() // safe opening ->name('js') // declare new storage space name ->url('somefile.css'); // add a new resource url to storage space
2.6.0.
Another relative method is urlOpen() which performs the same function as open()
method. The different however, is that urlOpen() is designed to remember the currently
selected (or declared) resource storage space name. Hence, it can be applied after Res::name() is called. An example is shown below:
// Method 3 ---------------------------------------------------- Res::name('css') //declare a storage space name ->urlOpen() //open in safe mode, remember storage space name ->url('somefile.css'); // add url to storage space name "css"
open() or urlOpen() are mostly used to reset global
custom settings made on urls back to their default state without affecting previously stored urls. This in most cases is used to reset a custom parent path
back to the default (empty) path set for certain specified url groups.
close() method is used to close declared resources or to entirely clear out all
previously stored urls. The example below is a good illustration of how the close method helps to close resources.
# Resource-File1.php ---------------------------------------------------- <?php Res::name('css') // declare new storage space name ->url('somefile1.css') // add a resource url to new storage space ->url('somefile2.css') // add a resource url to new storage space ->name('js') // declare new storage space name ->url('somefile1.js') // add a resource url to new storage space ->url('somefile2.js') // add a resource url to new storage space ->close(); // exit once from named into unamed storage space
close() method is used to exit from the named space.
In resource naming, there are two types of storage spaces which are unamed space and named space.
The unamed space is the top level space where urls can be stored without any group name. This space is the
default space where urls are saved if they are not assigned to any group name whatsoever. However, any url stored under group
storage space is assumed to be stored under the named space regardless of the group name assigned to such resource url. By calling the
Res::name() we simply shift into a named storage space and we can only exit back into the default unamed space using the Res::close()
or Res::open() method. In the sample above, the close() method is used to exit safely back into the default unamed storage space.
Res::close() method.
Aside from, exiting into a named storage space, the Res::close() can also be used to clear previously stored urls. The behaviour of the close()
method is that once it is called in an unamed space, it will assume that all previously stored urls are expected to be cleared. Due to this nature, developers may mistakenly clear
stored urls which may lead to resources not being imported. This event at most times do occur when the Res::close() method is called twice. An example is shown below:
<?php
Res::name('css') // declare new storage space name
->url('somefile1.css') // add a resource url to new storage space
->url('somefile2.css') // add a resource url to new storage space
->name('js') // declare new storage space name
->url('somefile1.js') // add a resource url to new storage space
->url('somefile2.js') // add a resource url to new storage space
->close() // named space exits into unamed storage space
->close(); // unamed space clears all previous urls and groups, starting afresh
close() method called will exit the previous named space into an unamed space. However,
in the second close() method, since we are no longer within a named storage space, the second
close() will clear all previous declarations and start afresh on a clean slate. This behaviour
is mostly the reason why it is preferable to use the Res::open() instead. In a situation where we need to start afresh
from a new file, the Res::open() is capable of achieving the same result when an argument of true is supplied as
shown below:
<?php Res::open(true) // clears all previous urls and groups, starting afresh ->name('js') // declare new storage space name ->url('somefile.css'); // add a new resource url to storage space
close() method is needed to clear previous urls, it may be more reasonable to indicate that the close()
method was indeed used to clear all urls. Rather than call the close() method twice, we can easily supply a true argument similarly to
the Res::open() method.
new()
method is used to return an instance of the Resource class, its major function is to set a parent path
that subsequent urls can inherit from. The new() method accepts a default url base as shown in the
example below:
<?php
Res::new('res/assets/') //set parent path
->name('file1')
->url('css/file1.css') // use parent path
->url('js/file1.js') // use parent path
->name('file2')
->url('res/main/css/file2.css') // use parent path
->url('res/main/js/file2.js') // use parent path
->close();
xurl() method makes this relaxation possible.
An example is shown below:
<?php
Res::new('res/assets/') //set custom parent path
->name('file1')
->url('css/file1.css') // use custom parent path
->xurl('res/main/js/file1.js') // ignore custom parent path (overide)
->url('css/file2.css') // use custom parent path
->name('file2')
->url('res/main/css/file2.css') // use custom parent path
->url('res/main/js/file2.js') // use custom parent path
->new('res/main') // new custom parent path
->name('file3')
->url('res/main/css/file3.css') // use new custom parent path
->close();
xurl() method. However, the full file path must be included into the xurl()
method. Every other url() declared will derive their base url from their parent new()
method just as shown above. If we need to change the parent path completely, we can use the new()
method to completely change the custom path.
Resource::path() function enables the mapping of a parent directory to all local urls supplied when working
with a parent and child directory. If static files (relative) urls are stored within the parent directory, a child directory
may have difficulty in importing the file except it uses the parent directory pointer ../ to pull the files. The
Resource::path() can be useful to declare in the child file that it needs to go to an upper directory. This must however
be declared before importing the files just as below.
Res::path('../');
Res::import();
Res class imports its static files from the res folder
. In order to prevent this, a resource ignore Res::ignore() is used to reset its path from the root
folder. The Res::ignore() method should come before the urls to be imported are stored. The close()
urlClose() open() or urlOpen() methods can be used to terminate this rule. It is however
preferable to avoid the use of close() methods.
Res::new()->name('css')
->url('folder/file.js')
Res::ignore();
Res::new()->name('js')
->url('folder/file.js')
->close();
Res::ignore() will only
affect the urls stored after it was called while those above will not be affected.
The urlClose() method will also terminate the effect of Res::ignore()
<?php
Res::name('scripts')
->url('http://site.com/js-file:::js')
->url('http://site.com/css-file:::css')
<script src="http://site.com/js-file">
<link href="http://site.com/css-file">
= & > sign (i.e => ) to point our attributes:
Res::name('scripts')
->url('http://site.com/js-file:::js => class:value; id: some_id')
<script src="http://site.com/js-file" class="value" id="some_id"></script>
; semicolon character. The script above is an example of the script tag format that will be returned when the
resource is imported.
named(), bind() and bindTo()
methods. Before a binding of resources is possible, each resource url should have its own unique name defined by the named() method. An example
of this is shown below:
<?php
Res::open()
->url('file1.css')->named('link1')
->url('file2.css')->named('link2');
Res::recall() method. As shown below
<?php
... continued ...
var_dump(Res::recall('link1'));
var_dump(Res::recall('link2'));
<link href="file1.css" rel="stylesheet" type="text/css">
<link href="file2.css" rel="stylesheet" type="text/css">
@recall() or the @load() directive to import resources through their unique names.
In the case where we need to store urls as groups, we can easily bind urls together either with bind() or bindTo() method.
The bind() method is used to bind two or more existing unique names together into a new unique name (or group). This is shown below:
<?php
Res::open()
->url('file1.css')->named('link1')
->url('file2.css')->named('link2')
->bind('links', ['link1', 'link2']); // merge both "link1" and "link2" into a new "link3"
var_dump(Res::recall('links'));
bind() method on that unique name. However, another method of great importance is the bindTo()
method. This method either merges existing unique groups together into a new group or it binds previously named resources into new
named group and flushes the binded values. A basic sample of how to use the bindTo() method is shown below:
<?php
Res::open()
->url('file1.css')->named('link1')
->url('file2.css')->named('link2')
->bindTo('links'); // bind previous url lists to a new group and reset list
var_dump(Res::recall('links'));
bindTo() method is used immediately after the two url declarations. This automatically tells the Resource class to
bind the previous resource urls together under a new unique name "links". This is an improvement over the previous bind() method.
Now, when we call the Res::recall(), both of the urls will be imported to the webpage in
the format below:
<link href="file1.css" rel="stylesheet" type="text/css">
<link href="file2.css" rel="stylesheet" type="text/css">
<?php
Res::open()
->url('file1.css')->named('file1')
->url('file2.css')->named('file2')
->bindTo('links1'); // bind "file1" and "file2" into "link1", reset list
->url('file3.css')->named('file3')
->url('file4.css')->named('file4')
->bindTo('links2'); // bind "file3" and "file4" into "link2", reset list
->url('file5.css')->named('file5')
->url('file6.css')->named('file6')
->bindTo('links1'); // add "file5" and "file6" into existing "link1", reset list
bindTo() method marks the end of previously named resource files.
This prevent resources files from accidentally falling into a wrong group.
The last line shows that we can easily bind new list of resource files into an existing group.
However, in certain cases, the bindTo() method may not reset previously named resource files.
This situation occurs when it is used to perform a different function which is the merging of two or more existing groups together.
An example is shown below:
<?php
Res::open()
->url('file1.css')->named('file1')
->url('file2.css')->named('file2')
->bindTo('links1'); // bind "file1" and "file2" and reset named list.
->url('file3.css')->named('file3')
->url('file4.css')->named('file4')
->bindTo('links2'); // bind "file3" and "file4" and reset named list
->url('file5.css')->named('file5')
->url('file6.css')->named('file6')
->bindTo('links3', ['links1','links2']) // merge "links1" and "links2" into "links3"
->bindTo('links4'); // bind "file5" and "file6" and reset named list
bindTo() was not used to bind previous
resource urls. Instead, it was rather used to merge two existing resource groups together.
In this situation, the unique name "links4" will still be able to bind the previous "file5"
and "file6" together because these resource urls are still available and have not been reset.
Although, the structure above may be valid, it is a much better practice to employ a more readable
structure to make it easier to read. The format above can be restructured as shown below:
<?php
Res::open()
->url('file1.css')->named('file1')
->url('file2.css')->named('file2')
->bindTo('links1'); // bind "file1" and "file2" and reset named list.
->url('file3.css')->named('file3')
->url('file4.css')->named('file4')
->bindTo('links2'); // bind "file3" and "file4" and reset named list
->url('file5.css')->named('file5')
->url('file6.css')->named('file6')
->bindTo('links4'); // bind "file5" and "file6" and reset named list
->bindTo('links3', ['links1','links2']) // merge "links1" and "links2" into "links3"
<?php
Res::open()
->url('file1.css')->name('f1')
->url('file2.css')->name('f2')
->bindTo('a-links'); // bind f1 & f2 as group
->url('file3.css')->name('f3')
->url('file4.css')->name('f4')
->bindTo('b-links'); // bind f3 & f4 as group
->url('file5.css')->name('f5')
->url('file6.css')->name('f6')
->bindTo('c-links.css')
->bind('d-links',['f5','a-links']); // merge "f5" and "a-links" (f5, f1 and f2).
var_dump() of the code above will resemple the format below"
var_dump(Res::recall('d-links')); //all items in "f5" and "a-links" (i.e file5.css, file1.css, file2.css)
<link href="http(s)://your_domain/file5.css" text="text/css" relativity="stylesheet">
<link href="http(s)://your_domain/file1.css" text="text/css" relativity="stylesheet">
<link href="http(s)://your_domain/file2.css" text="text/css" relativity="stylesheet">
Res::name() is used to name a resource file. In this situation, the Res::import()
method or the @import() directive are the relative helpers that can be used to import the stored resource files. Consider a resource file with code structure below:
<php?
Res::new('res/assets/')
-> name('anime')
-> url('css/anime.css')
-> url('js/anime.js')
-> name('design')
-> url('css/design.css')
-> url('js/design.js');
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>some template file</title>
@res(':anime') <-- import resources in anime group -->
</head>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>some template file</title>
@res(':design') <-- import resources in design group -->
</head>
Res::recall() method or
@load() directive can be used to import such binded resources. Consider the following binded
resources
<php?
Res::new('res/assets/')
-> url('css/anime.css')->named('animeCss')
-> url('js/anime.js')->named('animeJs')
-> bindTo('animeFiles');
<head>
<title>sample template file</title>
@load('animeCss') <--import animeCss file -->
@load('animeJs') <--import animeJs file -->
</head>
<head>
<title>sample template file</title>
@load('animeCss:animeJS') <--import animeCss and animeJs files -->
</head>
<head>
<title>sample template file</title>
@load('animeFiles') <--import animeCss and animeJs files -->
</head>
Res::name() method. It is much preferable to merge (or bind) resource files to storage names rather than
ordinary naming (or grouping) of files as this will make it much easier to control the flow of resources. There are
other means by which resource files can be imported. The code structure above is a resource file sample format
that will be used to provide more explanation on resource files importation.
Res::new('res/assets/')
-> name('anime')
-> url('css/anime.css')
-> url('js/anime.js')
-> name('design')
-> url('css/design.css')
-> url('js/design.js');
Res::import() method.
1. Res::import(':anime'); // import anime group 2. Res::import(':design'); // import design group 3. Res::import([':anime', ':design']); // import anime and design group 4. Res::import('res/',[':anime', ':design']); // import anime and design group, use parent path "res/"
line 1 & 2 are methods of importing stored groups
separately. The use of array in line 3 is a concise way of importing
multiple groups, without having to call the import() directive every time.
Files are resolved and imported in the way they are stored. In line 4, urls
are imported with parent url of res/ . However, this was used for testing purposes
hence not recommended for use. In template files, the @res() directive is mostly used for importing stored resources from the res/res.php resource
file.
Res::export() can be used. This can be also be achieved by using
Res::import(). Example below reveals how to export stored urls into variables
$urls = ":lists";
Res::import('', [':anime'], $urls);
var_dump($url);
$url was set with a value of
:lists, then supplied into the Res::import() method.
The directives tells the Resource class to update the value of $url
with the stored urls. Hence, $url was printed out to the page. Another way to export
stored urls is shown below:
$exports1 = Res::export([':anime']);
$exports2 = Res::export('../',[':anime']);
../ to imported local urls. The application of parent urls comes as a modification
to existing urls that may become invalid in static scripts. These helps to properly map the loaded urls to their
specific folders.
Res::recall() can be used. This method takes
two arguments. When the second argument is set as true, the script files will be returned in an array format,
making it easier to see the files that are stored in a unique group. Assuming we added a resource file below
Res::new('res/assets/')
-> url('css/anime.css')->named('animeCss')
-> url('js/anime.js')->named('animeJs')
->bind('animeCss');
$resources = Res::recall('animeCss', true);
var_dump($resource);
$resource will return an array of relative scripts stored. It is
important to note that relative paths to file are usually validated. Hence, the file must exist locally within
the application and also in the path provided before it can be successfully added to storage.
This helps to remove uneccesary resource links from the web application.