Hem
Välkommen

Welcome to Sandstorm

This is a simple MVC framework for building web applications in PHP. It's free and open source. This is for PHP developers who are beginners or professionals: This MVC framework I have built from the ground up and from my point of view on mvc architecture patterns. I will explain how this framework works, step by step, follow the instructions below.

app
Application Folder
2020-03-04
res
Resources of project
2020-03-04
site
Website Folder/File Structure
2020-03-04
upload
Upload Folder
2020-03-04
.htaccess
Config file of Sandstorm and site
2020-03-04
index.php
Launch of Sandstorm and Website
2020-03-04

Installation

  • 1 Download php and mysql
  • 2 Change in php.ini so it takes short tag
  • 3 Download Sandstorm to root folder of web server
  • 4 The first time you run Sandstorm, you will be taken to the welcome page.
  • 5 if you want to connect to your mysql. you must configure the pool file in Sandstorm
    you do that by going to app/config/mysql/default.db.php
    enter login credentials and the format of the data when you call from mysql
    of course you can have several pools, by copying and changing (default)
    to name you want in file name and class name.
    when you call the database class send with the name you specified.

Make new controller

To create a new controller in Sandstorm, start by navigating to the directory of your website. You have the flexibility to determine the structure of your project folder. For instance, if you decide to create a folder named "default," follow these steps:

  • 1 Create a folder named "default."
  • 2 Inside the created folder, generate a PHP file and name it appropriately, for example, "default.class.php."

When you open the newly created file, it should resemble the following:

<?

// First part of the file: Define the class name

class DefaultController extends IController {

// Second part of the file: Define the class and its methods

function index() {
// Add functionality for the index method here

}
}

Router

In web development, a router acts as a conductor, directing traffic between different parts of your web application. It's like a map that specifies which function should be executed when a user visits a particular web address (URL).

To specify a URL that should be associated with a function, you write a PHP comment in your code, something like this:

<?

// First part of the file: Define the class name

class DefaultController extends IController {

/**
* Start View
*
* @router / -> index
*/

function index() {
// Add functionality for the index method here

}
}

This comment states that when someone visits the root of your website ("/"), the "index" function should be called. It's like saying, "if someone goes to the main page, display the content from 'index'."

Sometimes, you want a more complex URL structure, like "user/blog/foo/1". To handle this, you write a router comment that describes how the URL should look. For example:

<?

// First part of the file: Define the class name

class UserController extends IController {

/**
* Start View
*
* @router user/blog/{string:slug}/{number(1-11):id} -> blog
*/

function blog() {
// Add functionality for the blog method here

}
}

This comment indicates that if someone visits a URL that looks like "user/blog/foo/1", the router function will understand that "foo" is a string and "1" is a number and which must be between 1-11 long. These values can then be used in your code to customize the content based on the user's preferences.

It's like creating a customized path through your web application based on how the user navigates through it.

When you start using Sandstorm to create your web application, it's a seamless and straightforward process. As you navigate to your URL, something cool happens automatically behind the scenes. Sandstorm dynamically generates settings in your htaccess file to control the traffic, and you're greeted with a blank page.

You don't have to worry about complex router configurations. The blank page you see when you visit your URL is like a clean canvas, ready for you to start creating and adding your own code.

Please note that this feature is specific to development mode, and you can find the generated settings in the htaccess file.

Views

When you step into the world of web development with Sandstorm, you'll soon discover two fundamental concepts: "Views" and "Data." These act like two friends collaborating to create the magic your users will experience.

Loading your initial view is like casting a spell with Sandstorm. Use $this->load("default/start.php") to open the door to your view. If the file is missing, Sandstorm will conjure the view when you visit the URL for the first time. Magic!

Now, when you want to send some information to your view, you've got a couple of tricks up your sleeve. To send data, you employ $this->setData('id', $id). Think of it as placing small messages or gifts in your view so it can utilize them when displayed.

Here's another interesting aspect. You can do it in two ways. Either you write $this->setData('id', $id)->("default/start.php"), which is like simultaneously providing information and opening your view, or you engage in a bit of wizardry and send multiple pieces of information, like $this->setData('id', $id, 'slug', $slug). It's akin to dispatching an entire map of clues to your view so it knows precisely what to display.

<?

// First part of the file: Define the class name

class DefaultController extends IController {

/**
* Start View
*
* @router / -> index
*/
function index() {

// Send data to the view
$this->setData('id', $id);

// Load the view, and if it doesn't exist, Sandstorm will create it
$this->load("default/start.php");

// Alternatively, you can do both in one line
$this->setData('id', $id)->load("default/start.php");

// Sending multiple pieces of data
$this->setData('id', $id, 'slug', $slug);

}
}

A tip:

if you write like this in the controller and then go to the URL, both settings for the router and the view will be created automatically. Make sure to validate the URL to ensure it works, and you will see 'hello world' in the browser.

<?

// First part of the file: Define the class name

class DefaultController extends IController {

/**
* Start View
*
* @router / -> index
*/
function index() {


// Load the view, and if it doesn't exist, Sandstorm will create it
$this->load("default/start.php");



}
}


Model

in Sandstorm there is model like other php mvc. if you go to app folder then dll folder which means Dynamic-link library and create a new example myModel.php then create a myModel class.

app/dll/myModel.php

<?

class myModel {

public function fetchData() {

// In a real-world scenario, this method would fetch data from a database or an external API
return "Hello from the model! Data fetched and ready to roll!";
}
}

Here, our model (MyModel) is a superhero that fetches data from the digital universe. Picture it like a data detective on a mission.

site/default/Default.class.php

<?


class DefaultController extends IController {

/**
* Start View
*
* @router / -> index
*/
function index() {
// Our controller is the director of the show, orchestrating the whole performance.

// Action! Cue the model!
$myModel = new MyModel();

// "FetchData" scene - where the model performs its magic
$dataFromModel = $myModel->fetchData();

// Cut! Now, let's load the view and pass the data to the audience (users).
$this->setData('data' $dataFromModel)->load("default/start.php"));
}
}

The controller (DefaultController) is the director of our MVC play. It commands the model to fetch the data and then instructs the view to present the scene to our audience.

site/default/start.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sandstorm MVC Adventure
</head>
<body>
<h1>Welcome to the Sandstorm MVC Adventure
<p> <?php echo $data; ?>


</body>
</html>

In the view (site/default/start.php), the stage is set. The welcome message is the grand opening, and the data from the model is the star of the show.

In the realm of Sandstorm, our model (MyModel) is like a digital superhero, going on a mission to fetch data from the vast internet world. It's the secret agent bringing back valuable information.

Our controller (DefaultController) is the director orchestrating the whole play. It commands the model to perform its magic and then instructs the view to present the grand scene to the audience (users).

Finally, the view is the stage where the magic happens. It welcomes users to the Sandstorm MVC Adventure and showcases the data fetched by our model. It's the visual representation of our digital story.