Chapter 10. Utilities

Table of Contents

Managing paths
Redirections and 404 errors

Managing paths

Atomik provides a single powerful method to manage paths: Atomik::path().

The method can be use in two differente ways. First to retreive a path or an array of path. In this case the first argument should be a set of paths (a string or an array containing paths). If only the first argument is given, the result will be a string containing the first path of the set. If the second argument is set to true, an array of paths will be returned.

Example 10.1. Using Atomik::path() with paths

				
echo Atomik::path('/tmp/data'); // /tmp/data
echo Atomik::path(array('/tmp/data', /tmp/data2')); // /tmp/data

paths = Atomik::path('/tmp/data', true); // array('/tmp/data')
$paths = Atomik::path(array('/tmp/data', /tmp/data2'), true); // array('/tmp/data', /tmp/data2')
			

The same method can also be use to find the pathname of a file. In this case, the first argument must be a string containing the file's name. The second argument must be a set of paths (a string or an array).

The method will search in paths from the set of paths and return the first found file with the file name provided as the first argument. If no file is found, false is returned.

It is possible to avoid checking for the file existence by passing false as the third argument. This will however always return the file as if it was in the first path of the set of paths.

Example 10.2. Using Atomik::path() to find files

				
$pathname = Atomik::path('my-file.txt', array('/path1', '/path2')); // /path1/my-file.txt if the file is located in /path1 (/path2 otherwise)
$pathname = Atomik::path('my-file.txt', '/path1'); // /path1/my-file.txt

			

Redirections and 404 errors

To redirect the user to another page you can use the Atomik::redirect() method. It takes as argument the url to redirect to. By default, this url will first be process using Atomik::url(). This behaviour can be disable by passing false as the second argument.

Example 10.3. Redirecting

				
Atomik::redirect('home');
			

Triggering 404 errors is even simpler. Just call the Atomik::trigger404() method. Customization of the 404 error page has been discussed in the configuration chapter.

Example 10.4. Trigerring a 404 error

				
Atomik::trigger404();