Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/lukewatts/thistle

Thistle is a small, modular MVC framework built on Silex
https://github.com/lukewatts/thistle

Last synced: 4 days ago
JSON representation

Thistle is a small, modular MVC framework built on Silex

Awesome Lists containing this project

README

        

## Thistle Framework

**Name:** Thistle

**Author:** Affinity4

The Thistle Framework is built on the [Silex framework v1.3](http://silex.sensiolabs.org/doc/1.3). However, it comes with Controllers, Models, Views, a config file and routes already set up to use immediately.

### Views
Views are setup to use Twig. You can use them in your controllers with the ```view()``` function like so:

```
// app/routes.php
$app->get('/', 'App\Controller\Page::home')-bind('home');
```

```
// app/controllers/Page.php
namespace App\Controller;

use Silex\Application;

class Page extends BaseController
{
public function home(Application $app)
{
return view('home', [
'message' => 'Hello Wordl!'
]);
}
}
```

```
// app/views/home.html.twig

{{ message }}

{{ message }}

```

### Models (DEPRECATED as of v0.0.6)
You can easily create a Model and attach it to ```$app``` by first simply a file in the app/models directory:

```
// app/models
namespace App\Model;

class User extends BaseModel
{
public function getTableName()
{
return 'users';
}
}
```

That's all you need to do to tell this object to map to the 'users' table. Thistle will handle the rest.

You'll now have access to that object from ```$app['user']```. You can use this to retrieve rows from the users table, insert, delete and update.

Note: The keys are created from the name of the file/class in the app/models directory.
For example, if you're class is called UserPage, you will access that table using the ```$app['user_page']```

### Entities
Place Entities in the `app/entities` folder.

```
namespace Thistle\App\Entity;

/**
* Class User
* @package App\Entity
* @Entity
* @Table(name="users")
*/
class User {
/**
* @var
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
private $id;

/**
* @var
* @Column(type="string")
*/
private $name;

/**
* @var
* @Column(type="datetime")
*/
private $created_at;

/**
* @var
* @Column(type="datetime")
*/
private $updated_at;

/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}

/**
* @return mixed
*/
public function getId()
{
return $this->id;
}

/**
* @param mixed $name
*/
public function setName($name)
{
$this->name = $name;
}

/**
* @return mixed
*/
public function getName()
{
return $this->name;
}

/**
* @param mixed $created_at
*/
public function setCreatedAt($created_at)
{
$this->created_at = $created_at;
}

/**
* @return mixed
*/
public function getCreatedAt()
{
return $this->created_at;
}

/**
* @param mixed $updated_at
*/
public function setUpdatedAt($updated_at)
{
$this->updated_at = $updated_at;
}

/**
* @return mixed
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
}

```

#### Creating the Database Tables from defined Entities
You can then create the table using the `thistle` console command from the project root:

```
thistle orm:schema-tool:create
```

#### Inserts
To insert a new row you can create a new object and user the setter methods you defined to insert fields:

```
$User = new Thistle\App\Entity\User();
$User->setName('Luke');
$User->setCreatedAt(new \DateTime('now'));
$User->setUpdatedAt(new \DateTime('now'));

$app['em']->persist($User);
```

Once you are ready to save you can call the flush method on the entity manager;

```
$app['em']->flush();
```

#### SELECT

To find a row by ID use:

```
$app['em']->find('Thistle\App\Entity\User', 1); // SELECT * FROM users WHERE id = 1 LIMIT 1
```

### Useful Methods when using Entities
```
// SELECT * FROM users WHERE id = 1 LIMIT 1
$app['em']->find('Thistle\App\Entity\User', 1) // Finds the user with id of 1
```

Would return:
```
[
'id' => '1',
'name' => 'Luke Watts',
'type' => 'Admin',
...
]
```

```
// SELECT * FROM users WHERE name = "Luke Watts" LIMIT 1
$app['em']->getRepository('Thistle\App\Entity\User')->findOneBy(['name' => 'Luke Watts']) // Returns one row where name = "Luke Watts"
```
Would return:
```
[
'id' => '1',
'name' => 'Luke Watts',
'type' => 'Admin'
]
```
```
// SELECT * FROM users WHERE name
$app['em']->getRepository('Thistle\App\Entity\User')->findBy(['type' => "standard"]); // Returns array of users where type = "standard"
```
Would return
```
[
0 => [
'id' => '2',
'name' => 'Jane Doe',
'type' => 'standard',
],
1 => [
'id' => '3',
'name' => 'John Doe',
'type' => 'standard',
]
]
```

### Password Encryption
Passwords can be encrypted and verified through the `$app['password']` service provider. This uses the native PHP `password_hash` and `password_verify` functions with the `PASSWORD_BCRYPT` algorithm by default.

#### Password::make()
To generate and return a password hash use:

```
$app['password']->make('P@55w0rd'); // Returns $2y$09$ts2u4/Dis4W4YqxOdgufyuAuAY4zaYfPRw5p5EainFfQ05Or/KYAy
```

#### Password::verfiy()
To verify a password you first need to pass the plain unencrypted password and the hashed password to the `$app['password']::verify()` method:

```
$User = $app['em']->getRepository('Thistle\App\Entity\User')->findOneBy(['username' => 'LukeWatts']);

$app['password']->verify('P@55w0rd', $User->getPassword());

```

#### Password Cost
By default the `$app['password']->make()` method will autodetect the best 'cost' for your current servers configuration. This will override the cost set in the `providers.php` file.

To set your own cost you first should define it in the `Thistle\App\Core\Provider\Password\Password` parameters array in `app/providers.php`:

```
'Thistle\App\Core\Provider\Password\Password' => [
'password.cost' => 12
],
```

Then when using `$app['password']->make()` you will need to set the second parameter `$auto_optimize_cost` to `false`:

```
$app['password']->make('P@55w0rd', false);
```

___NOTE: setting the password.cost value too high will slow down load speeds whenever `$app['password']->make()` is used.___

### Routing and Controllers
Routes can be found in the app/routes.php file. For more information on routes see the [Silex routing documentation](http://silex.sensiolabs.org/doc/1.3/usage.html#routing)

```
$app->get('/', 'App\Controller\Page::home')->bind('home');
```
This would map to the method "home" in the controller "Page" found in app/controllers/Page.php.

You can also use closure instead of controllers if you like for smaller routes.
```
$app->get('/test', function () use ($app) {
return new Symfony\Component\HttpFoundation\Response('Success!');
})->bind('test');
```

### Silex
For anything else you can see the [Silex Documentation](http://silex.sensiolabs.org/doc/1.3) seeing as Thistle is mostly a boilerplate for Silex.

### Licence
Licenced under GNU GPLv3