Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Description

...

Core\Storage storage()

...

Storage Our storage function allows you to store information in the DB, without the need to create custom database tables on a clients set. Its based on a key/value system that allows for custom queries and ordering plus the ability to set and return multiple values for a specific key.

Examples

...

Code Block
languagephp
<?php

route('/foo', function() {

   /**
    * Set a string value "bar" in the DB using the key "foo"
    */
   storage()->set('foo', 'bar');

   // Print the value for "foo"
   $object = storage()->get('foo');

   echo $object->value;



   /**
    * Storing arrays
    */
   storage()->set('foo_array', [
      'hello' => 'world'
   ]);

   // Print the value for "foo_array"
   $object = storage()->get('foo_array');

   // Prints the object
   print_r($object->value);

   // Print the "hello" value in our array, which will output "world"
   echo $object->value->hello;



   /**
    * Storing multiple values
    */
   storage()->set('multiple', '1');
   storage()->set('multiple', '2');
   storage()->set('multiple', '3');

   // Get all objects based on key
   $objects = storage()->all('multiple');
   foreach ($objects as $object) {
      // Will print 1,2,3
      echo $object->value . ',';
   }
});

...

segment(int $number)

 

$number

Provide the segment number
of the URL. 

MethodDescriptionUsageReturns

get(string $var $key [, int $id = null] )

 

$var

Provide the HTTP request key

Returns the HTTP request value.

$key

Unique storage key.


$id

If you store multiple data in a key
you can pass a unique ID to return a
specific item from that data set. 

 

Get data from the DB based on a unique key
and/or ID. 

Code Block
languagephp
/**
 * URL Example: /foo?hello=1
 */
if (request($object = storage()->get('hellofoo'));
{
   echo "Hello!";
}
HTTP request value on success
$object->value;

On success it returns Core\Storage\Object, null on failure.

This supports all incoming HTTP
requests (e.g. GET, POST, DELETE etc...) 

isPost()

Checks to see if a form has been posted.getById(int $id)

 


$id

Unique auto incremented ID.

Get a specific data set based on the unique
auto incremented key created by Core\Storage\Object. 

Code Block
languagephp
if$object (request()->isPost()) {

}

true on success, false on failure.

This method only checks POST requests.

Returns URL segment based on the
forward slash. 

 

Code Block
languagephp
/**
 * URL Example: /foo/bar
 */
echo request()->segment(1); // Prints foo
echo request()->segment(2); // Prints bar
Segment value on success, null
= storage()->getById(1);
 
echo $object->value;

On success it returns Core\Storage\Object, null on failure.

all(string $key)

 


$key

Unique storage key.


Returns all data sets based on a unique key.

 

Code Block
$objects = storage()->all('multiple');


foreach ($objects as $object) {
 echo $object->value;
}

Array of values on success, empty array on failure.

Note: Each value from the array is an object from Core\Storage\Object

set(string $key, mixed $value , int $id = 0] )

 


$key

Unique storage key.


$value

Value to set for this entry. Can be a string, array, int or boolean.


$id

(optional) Include a custom unique ID for this data set.

 Sets data into the DB based on your key.
Code Block
// Set a string
storage()->set('foo', 'bar');
 
// Set an array
storage()->set('foo_array', [
   'hello' => 'world'
]);
 
// Set multiple values
storage()->set('foo', '1');
storage()->set('foo', '2');
storage()->set('foo', '3');
Returns unique auto increment ID on success, false on failure.