Your first PHPDevShell script
Did you read the introduction yet? Now that you know that your first script will be in your own plugin, and that a menu connects the user to this script. Lets look at how we can start using PHPDevShell methods and function and merge with its shared environment. You will be happy to learn all the information and powerful methods you have at your fingertips. Once you learn how methods are divided into, you will be coding in no time!
How do you get shared resources?
PHPDevShell divides its classes and instances up in categories for your plugin to share. Not only does it keep information there it also keeps the most useful methods at your fingertips. It uses lazy loading so you don't have to worry about including incorrect files etc.
The core classes
At first you will be using methods from the 5 most prominent classes. These classes gives you enough power to deliver most applications. They are all stored in ready to use instances and will be available to you instantly, wherever you are coding. So first thing is first, lets have a quick look at the most used class instances you will have available in your script;
$this->db $this->navigation $this->security $this->template $this->user $this->tagger $this->core // And an array with a whole lot of session information; $this->configuration
Click on the headings to see all the methods per class as we will only cover single examples in each method.
$this->configuration
The configuration class will give you an array of information regarding the current logged in session. Lets look at some array information it stores;
<?php echo $this->configuration['user_display_name']; // Will show logged in users name. // Actually it contains allot of useful information; printr($this->configuration); ?>
$this->core
The core contains methods that does not really fit anywhere else, and focuses on simple methods but which needs to be handled in specific ways. For instance, lets say we want to show the current date and time in a settings formatted way;
<?php // It contains many methods, this is just one example; // This might sound simple, but PHPDevShell keeps track of the timezone. $this->core->formatTimeDate(time()); ?>
$this->tagger
The tagger class is an advanced class to group core elements, users, groups, roles and menus by tags and recalling them as a grouped unit when needed.
// It contains many methods, this is just one example;
$this->tagger->tag('user', $id, 'admin', 'Administrator');
$this->tagger->tagLookup('user', $id);
<?php
?>
$this->user
The user class contains methods to deal with users, it contains many useful administrator methods to deal with everyday
<?php // It contains many methods, this is just one example; ?>
$this->template
The template method is used for displaying styled elements inside your script, it is generally styled from the main theme. It also contains very powerful methods falling inside this category.
<?php
// It contains many methods, this is just one example;
// Lets create a heading for a script.
$this->template->heading('My first script');
if (empty($this->configuration['user_email'])) {
// This will display a neat warning message and log it with the database logger.
$this->template->warning('Oops, you have no email');
}
?>
$this->security
The security class is mostly automated these days and you will use very few methods from it, but it does contain a few oldies that you might use.
<?php // It contains many methods, this is just one example; // Lets encrypt a string. $foo = 'Hello World'; $encrypt = $this->security->encrypt($foo); $foo = $this->security->decrypt($encrypt); // Contains 'Hello World' ?>
$this->navigation
The navigation methods contains very powerful methods to handle everything regarding your sites navigation, menus and seo. PHPDevShell works in the unique way of only knowing what the logged in user knows or is allowed to know. So if a user can only see Menu A, B, C the navigation model will only contain this for the user.
<?php
// It contains many methods, this is just one example;
// Lets see the current array of menu information and the data the active logged in user can view.
print_r($this->navigation->navigation);
// There are useful methods of always using the correct url independent of SEO settings.
echo "<a href={$this->navigation->buildURL('1231231231')}>Some Existing Url</a>";
?>
$this->db
The db is all about database and data access, it contains also all the important query methods. So when it comes to accessing the database, this is where you will look for tools. Note ORM does not work from here as it is an independent plugin.
<?php
// It contains many methods, this is just one example;
$query = $this->db->newQuery('SELECT * FROM database');
?>
Now that we have all these tools, lets build a proper script.
By this time, just to clarify where you should be standing when starting to code your first script;
- Create a plugin folder under /plugins/MyFirstPlugin/
- Create a file that you will be working inside the just created folder /plugins/MyFirstPlugin/first.php
- Create a Plugin Type 1 menu linking to it from the Menu manager
- Still unsure? Read this guide
Great you now have an empty php file and you can now start using the available PHPDevShell classes to create your first application. Even though your first application will run inside the default application type theme, your script will be able to run in any theme you create later on.
Going forward we will now call your script the controller. The correct way is to wrap your controller code inside the controller class of PHPDevShell. However, this is not even mandatory, you can still write your PHP code directly without extending the main PHPDevShell controller.
<?php
class myFirstApp extends PHPDS_controller
{
// execute is the default controller method.
public function execute()
{
// Your script code goes here...
}
}
return 'myFirstApp';
?>
Ok, this was simple enough, now lets add a neat heading and some intro text to our application using some
$this->template
methods.
<?php
class myFirstApp extends PHPDS_controller
{
// execute is the default controller method.
public function execute()
{
$this->template->heading('My First App');
$this->template->info('This is just my first sandbox application in PHPDevShell, I like what I see so far!');
}
}
return 'myFirstApp';
?>
By now your application should look something like this:

Normally I wont add html in my controller, this really belongs in the view, but for this example we will do it for simplicities sake. For now we will just kinda keep the html and controller seperated in two different methods. Lets create the form in the same controller class and call the method myFirstForm;
<?php
class myFirstApp extends PHPDS_controller
{
// execute is the default controller method.
public function execute()
{
$this->template->heading('My First App');
$this->template->info('This is just my first sandbox application in PHPDevShell, I like what I see so far!');
$this->myFirstForm();
}
private function myFirstForm()
{
// Lets create some form.
$FORM_HTML = <<
HTML;
echo $FORM_HTML;
}
}
return 'myFirstApp';
?>
Once we are done with this, lets create a method to handle the form when the submit button is pushed.
<?php
class myFirstApp extends PHPDS_controller
{
// execute is the default controller method.
public function execute()
{
$this->template->heading('My First App');
$this->template->info('This is just my first sandbox application in PHPDevShell, I like what I see so far!');
$this->handleForm();
$this->myFirstForm();
}
private function myFirstForm()
{
// Lets create some form.
$FORM_HTML = <<
HTML;
echo $FORM_HTML;
}
private function handleForm()
{
if (! empty($_POST)) {
if (! empty($_POST['name'])) {
$this->template->ok(sprintf("Thanks girlfriend, the name {$_POST['name']} was submitted."));
} else {
$this->template->warning('Are you sick in your head, no name was given!?');
}
}
}
}
return 'myFirstApp';
?>
Mmm... this is really it, now if you hit the submit button the handleForm method will detect it and handle the data. Now you have a clean, protected first application in PHPDevShell. So your end results should be something like this too:

