mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Add namespaces
This commit is contained in:
parent
33be3a0c34
commit
e7274b0ee4
@ -547,6 +547,7 @@ when you want to find all the members that does not exist in a Group.
|
||||
|
||||
```php
|
||||
// ... Finding all members that does not belong to $group.
|
||||
use SilverStripe\Security\Member;
|
||||
$otherMembers = Member::get()->subtract($group->Members());
|
||||
```
|
||||
|
||||
@ -555,6 +556,7 @@ when you want to find all the members that does not exist in a Group.
|
||||
You can limit the amount of records returned in a DataList by using the `limit()` method.
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Member;
|
||||
$members = Member::get()->limit(5);
|
||||
```
|
||||
|
||||
@ -582,6 +584,8 @@ For instance, the below model will be stored in the table name `BannerImage`
|
||||
|
||||
```php
|
||||
namespace SilverStripe\BannerManager;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
|
||||
class BannerImage extends \DataObject
|
||||
{
|
||||
private static $table_name = 'BannerImage';
|
||||
@ -616,6 +620,9 @@ table and column.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
|
||||
public function countDuplicates($model, $fieldToCheck)
|
||||
{
|
||||
$table = DataObject::getSchema()->tableForField($model, $field);
|
||||
@ -722,7 +729,7 @@ The data for the following classes would be stored across the following tables:
|
||||
|
||||
```yml
|
||||
|
||||
SiteTree:
|
||||
SilverStripe\CMS\Model\SiteTree:
|
||||
- ID: Int
|
||||
- ClassName: Enum('SiteTree', 'Page', 'NewsPage')
|
||||
- Created: Datetime
|
||||
|
@ -412,6 +412,8 @@ Relationships between objects can cause cascading deletions, if necessary, throu
|
||||
`cascade_deletes` config on the parent class.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\DataObject;
|
||||
|
||||
class ParentObject extends DataObject {
|
||||
private static $has_one = [
|
||||
'Child' => ChildObject::class,
|
||||
|
@ -12,6 +12,8 @@ modify.
|
||||
[SS_List](api:SilverStripe\ORM\SS_List) implements `IteratorAggregate`, allowing you to loop over the instance.
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Member;
|
||||
|
||||
$members = Member::get();
|
||||
|
||||
foreach($members as $member) {
|
||||
|
@ -173,6 +173,7 @@ On the most basic level, the class can be used as simple conversion class from o
|
||||
number.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
DBField::create_field('Double', 1.23456)->Round(2); // results in 1.23
|
||||
```
|
||||
|
||||
@ -180,6 +181,7 @@ Of course that's much more verbose than the equivalent PHP call. The power of [D
|
||||
sophisticated helpers, like showing the time difference to the current date:
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\FieldType\DBField;
|
||||
DBField::create_field('Date', '1982-01-01')->TimeDiff(); // shows "30 years ago"
|
||||
```
|
||||
|
||||
|
@ -47,7 +47,7 @@ config:
|
||||
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
DataListFilter.CustomMatch:
|
||||
class: MyVendor/Search/CustomMatchFilter
|
||||
class: MyVendor\Search\CustomMatchFilter
|
||||
```
|
||||
|
||||
The following is a query which will return everyone whose first name starts with "S", either lowercase or uppercase:
|
||||
|
@ -19,6 +19,10 @@ For example, if you want to run a simple `COUNT` SQL statement,
|
||||
the following three statements are functionally equivalent:
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use SilverStripe\Security\Member;
|
||||
|
||||
// Through raw SQL.
|
||||
$count = DB::query('SELECT COUNT(*) FROM "Member"')->value();
|
||||
|
||||
@ -96,6 +100,7 @@ object instead.
|
||||
For example, creating a `SQLDelete` object
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLDelete;
|
||||
|
||||
$query = SQLDelete::create()
|
||||
->setFrom('"SiteTree"')
|
||||
@ -107,6 +112,7 @@ For example, creating a `SQLDelete` object
|
||||
Alternatively, turning an existing `SQLSelect` into a delete
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
|
||||
$query = SQLSelect::create()
|
||||
->setFrom('"SiteTree"')
|
||||
@ -119,6 +125,7 @@ Alternatively, turning an existing `SQLSelect` into a delete
|
||||
Directly querying the database
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\DB;
|
||||
|
||||
DB::prepared_query('DELETE FROM "SiteTree" WHERE "SiteTree"."ShowInMenus" = ?', [0]);
|
||||
|
||||
@ -169,6 +176,8 @@ SQLInsert also includes the following api methods:
|
||||
E.g.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLUpdate;
|
||||
|
||||
$update = SQLUpdate::create('"SiteTree"')->addWhere(['ID' => 3]);
|
||||
|
||||
// assigning a list of items
|
||||
@ -202,6 +211,8 @@ these are translated internally as multiple single row inserts.
|
||||
For example,
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLInsert;
|
||||
|
||||
$insert = SQLInsert::create('"SiteTree"');
|
||||
|
||||
// Add multiple rows in a single call. Note that column names do not need
|
||||
@ -232,6 +243,8 @@ e.g. when you want a single column rather than a full-blown object representatio
|
||||
Example: Get the count from a relationship.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
|
||||
$sqlQuery = new SQLSelect();
|
||||
$sqlQuery->setFrom('Player');
|
||||
$sqlQuery->addSelect('COUNT("Player"."ID")');
|
||||
@ -255,6 +268,9 @@ This can be useful for creating dropdowns.
|
||||
Example: Show player names with their birth year, but set their birth dates as values.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
use SilverStripe\Forms\DropdownField;
|
||||
|
||||
$sqlQuery = new SQLSelect();
|
||||
$sqlQuery->setFrom('Player');
|
||||
$sqlQuery->setSelect('Birthdate');
|
||||
|
@ -174,6 +174,8 @@ By default, all records are retrieved from the "Draft" stage (so the `MyRecord`
|
||||
explicitly request a certain stage through various getters on the `Versioned` class.
|
||||
|
||||
```php
|
||||
use SilverStripe\Versioned\Versioned;
|
||||
|
||||
// Fetching multiple records
|
||||
$stageRecords = Versioned::get_by_stage('MyRecord', Versioned::DRAFT);
|
||||
$liveRecords = Versioned::get_by_stage('MyRecord', Versioned::LIVE);
|
||||
|
@ -50,6 +50,8 @@ class MyCustomController extends Controller
|
||||
### CSS Files
|
||||
|
||||
```php
|
||||
use SilverStripe\View\Requirements;
|
||||
|
||||
Requirements::css($path, $media);
|
||||
```
|
||||
|
||||
|
@ -11,6 +11,8 @@ The `PaginatedList` will automatically set up query limits and read the request
|
||||
**mysite/code/Page.php**
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\PaginatedList;
|
||||
|
||||
/**
|
||||
* Returns a paginated list of all pages in the site.
|
||||
*/
|
||||
@ -79,6 +81,8 @@ will break the pagination. You can disable automatic limiting using the [Paginat
|
||||
when using custom lists.
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\PaginatedList;
|
||||
|
||||
$myPreLimitedList = Page::get()->limit(10);
|
||||
|
||||
$pages = new PaginatedList($myPreLimitedList, $this->getRequest());
|
||||
|
@ -33,13 +33,15 @@ situations, you can disable anchor link rewriting by setting the `SSViewer.rewri
|
||||
**mysite/_config/app.yml**
|
||||
|
||||
```yml
|
||||
SSViewer:
|
||||
SilverStripe\View\SSViewer:
|
||||
rewrite_hash_links: false
|
||||
```
|
||||
|
||||
Or, a better way is to call this just for the rendering phase of this particular file:
|
||||
|
||||
```php
|
||||
use SilverStripe\View\SSViewer;
|
||||
|
||||
public function RenderCustomTemplate()
|
||||
{
|
||||
SSViewer::setRewriteHashLinks(false);
|
||||
|
@ -16,6 +16,9 @@ Creating a [Form](api:SilverStripe\Forms\Form) has the following signature.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
|
||||
$form = new Form(
|
||||
$controller, // the Controller to render this form on
|
||||
$name, // name of the method that returns this form on the controller
|
||||
|
@ -22,6 +22,8 @@ The `SecurityToken` automatically added looks something like:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\Form;
|
||||
|
||||
$form = new Form(..);
|
||||
echo $form->getSecurityToken()->getValue();
|
||||
|
||||
|
@ -11,6 +11,8 @@ To make an entire [Form](api:SilverStripe\Forms\Form) read-only.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\Form;
|
||||
|
||||
$form = new Form(..);
|
||||
$form->makeReadonly();
|
||||
```
|
||||
@ -19,6 +21,8 @@ To make all the fields within a [FieldList](api:SilverStripe\Forms\FieldList) re
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\FieldList;
|
||||
|
||||
$fields = new FieldList(..);
|
||||
$fields = $fields->makeReadonly();
|
||||
```
|
||||
@ -27,6 +31,9 @@ To make a [FormField](api:SilverStripe\Forms\FormField) read-only you need to kn
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\Forms\FieldList;
|
||||
|
||||
$field = new TextField(..);
|
||||
$field = $field->performReadonlyTransformation();
|
||||
|
||||
|
@ -103,6 +103,8 @@ transparently generate the relevant underlying TinyMCE code.
|
||||
**mysite/_config.php**
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\HTMLEditor\HtmlEditorConfig;
|
||||
|
||||
HtmlEditorConfig::get('cms')->enablePlugins('media');
|
||||
```
|
||||
|
||||
@ -270,6 +272,8 @@ Example: Remove field for "image captions"
|
||||
|
||||
```php
|
||||
// File: mysite/_config.php
|
||||
use SilverStripe\Admin\ModalController;
|
||||
|
||||
ModalController::add_extension('MyToolbarExtension');
|
||||
```
|
||||
|
||||
|
@ -8,6 +8,8 @@ tabular data in a format that is easy to view and modify. It can be thought of a
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridField;
|
||||
|
||||
$field = new GridField($name, $title, $list);
|
||||
```
|
||||
|
||||
@ -103,6 +105,9 @@ the `getConfig()` method on `GridField`.
|
||||
|
||||
With the `GridFieldConfig` instance, we can modify the behavior of the `GridField`.
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridFieldConfig;
|
||||
use SilverStripe\Forms\GridField\GridFieldDataColumns;
|
||||
|
||||
// `GridFieldConfig::create()` will create an empty configuration (no components).
|
||||
$config = GridFieldConfig::create();
|
||||
|
||||
@ -115,7 +120,10 @@ With the `GridFieldConfig` instance, we can modify the behavior of the `GridFiel
|
||||
|
||||
`GridFieldConfig` provides a number of methods to make setting the configuration easier. We can insert a component
|
||||
before another component by passing the second parameter.
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridFieldFilterHeader;
|
||||
|
||||
$config->addComponent(new GridFieldFilterHeader(), 'GridFieldDataColumns');
|
||||
```
|
||||
|
||||
@ -169,6 +177,8 @@ A simple read-only and paginated view of records with sortable and searchable he
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridFieldConfig_Base;
|
||||
|
||||
$config = GridFieldConfig_Base::create();
|
||||
|
||||
$gridField->setConfig($config);
|
||||
@ -199,6 +209,8 @@ this record.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridFieldConfig_RecordViewer;
|
||||
|
||||
$config = GridFieldConfig_RecordViewer::create();
|
||||
|
||||
$gridField->setConfig($config);
|
||||
@ -354,6 +366,9 @@ bottom right of the table.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridFieldButtonRow;
|
||||
use SilverStripe\Forms\GridField\GridFieldPrintButton;
|
||||
|
||||
$config->addComponent(new GridFieldButtonRow('after'));
|
||||
$config->addComponent(new GridFieldPrintButton('buttons-after-right'));
|
||||
```
|
||||
@ -366,6 +381,8 @@ create an area rendered before the table wrapped in a simple `<div>`.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
||||
|
||||
class MyAreaComponent implements GridField_HTMLProvider
|
||||
{
|
||||
|
||||
@ -389,6 +406,8 @@ Now you can add other components into this area by returning them as an array fr
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
|
||||
|
||||
class MyShareLinkComponent implements GridField_HTMLProvider
|
||||
{
|
||||
|
||||
|
@ -19,6 +19,11 @@ below:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\GridField\GridField_ColumnProvider;
|
||||
use SilverStripe\Forms\GridField\GridField_ActionProvider;
|
||||
use SilverStripe\Forms\GridField\GridField_FormAction;
|
||||
use SilverStripe\Control\Controller;
|
||||
|
||||
class GridFieldCustomAction implements GridField_ColumnProvider, GridField_ActionProvider
|
||||
{
|
||||
|
||||
|
@ -43,6 +43,11 @@ There's quite a bit in this function, so we'll step through one piece at a time.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\FieldList;
|
||||
use SilverStripe\Forms\TextField;
|
||||
use SilverStripe\Forms\EmailField;
|
||||
use SilverStripe\Forms\TextareaField;
|
||||
|
||||
$fields = new FieldList(
|
||||
new TextField('Name'),
|
||||
new EmailField('Email'),
|
||||
@ -137,6 +142,9 @@ The framework comes with a predefined validator called [RequiredFields](api:Silv
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\RequiredFields;
|
||||
|
||||
public function Form()
|
||||
{
|
||||
// ...
|
||||
|
@ -210,9 +210,9 @@ The structure of each YAML file is a series of headers and values separated by Y
|
||||
- '#rootroutes'
|
||||
- '#coreroutes'
|
||||
---
|
||||
Director:
|
||||
SilverStripe\Control\Director:
|
||||
rules:
|
||||
'admin': 'AdminRootController'
|
||||
'admin': 'SilverStripe\Admin\AdminRootController'
|
||||
---
|
||||
```
|
||||
|
||||
@ -260,9 +260,9 @@ keys is a list of reference paths to other value sections. A basic example:
|
||||
- '#rootroutes'
|
||||
- '#coreroutes'
|
||||
---
|
||||
Director:
|
||||
SilverStripe\Control\Director:
|
||||
rules:
|
||||
'admin': 'AdminRootController'
|
||||
'admin': 'SilverStripe\Admin\AdminRootController'
|
||||
---
|
||||
```
|
||||
|
||||
|
@ -49,7 +49,7 @@ we want to add the `MyMemberExtension` too. To activate this extension, add the
|
||||
|
||||
```yml
|
||||
|
||||
Member:
|
||||
SilverStripe\Security\Member:
|
||||
extensions:
|
||||
- MyMemberExtension
|
||||
```
|
||||
@ -58,7 +58,7 @@ Alternatively, we can add extensions through PHP code (in the `_config.php` file
|
||||
|
||||
|
||||
```php
|
||||
Member::add_extension('MyMemberExtension');
|
||||
SilverStripe\Security\Member::add_extension('MyMemberExtension');
|
||||
```
|
||||
|
||||
This class now defines a `MyMemberExtension` that applies to all `Member` instances on the website. It will have
|
||||
@ -130,6 +130,8 @@ we added a `SayHi` method which is unique to our extension.
|
||||
**mysite/code/Page.php**
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Security;
|
||||
|
||||
$member = Security::getCurrentUser();
|
||||
echo $member->SayHi;
|
||||
|
||||
|
@ -38,6 +38,8 @@ Other fields can be manually parsed with shortcodes through the `parse` method.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\View\Parsers\ShortcodeParser;
|
||||
|
||||
$text = "My awesome [my_shortcode] is here.";
|
||||
ShortcodeParser::get_active()->parse($text);
|
||||
```
|
||||
|
@ -20,6 +20,8 @@ The following sums up the simplest usage of the `Injector` it creates a new obje
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$object = Injector::inst()->create('MyClassName');
|
||||
```
|
||||
|
||||
@ -30,7 +32,7 @@ The benefit of constructing objects through this syntax is `ClassName` can be sw
|
||||
|
||||
```yml
|
||||
|
||||
Injector:
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
MyClassName:
|
||||
class: MyBetterClassName
|
||||
```
|
||||
@ -134,7 +136,7 @@ As well as properties, method calls can also be specified:
|
||||
|
||||
```yml
|
||||
|
||||
Injector:
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
Logger:
|
||||
class: Monolog\Logger
|
||||
calls:
|
||||
@ -169,7 +171,7 @@ An example using the `MyFactory` service to create instances of the `MyService`
|
||||
|
||||
```yml
|
||||
|
||||
Injector:
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
MyService:
|
||||
factory: MyFactory
|
||||
```
|
||||
@ -247,7 +249,7 @@ And the following configuration..
|
||||
MyController:
|
||||
dependencies:
|
||||
permissions: %$PermissionService
|
||||
Injector:
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
PermissionService:
|
||||
class: RestrictivePermissionService
|
||||
properties:
|
||||
|
@ -95,6 +95,8 @@ Linking to resources in vendor modules uses exactly the same syntax as non-vendo
|
||||
this is how you would require a script in this module:
|
||||
|
||||
```php
|
||||
use SilverStripe\View\Requirements;
|
||||
|
||||
Requirements::javascript('tractorcow/test-vendor-module:client/js/script.js');
|
||||
```
|
||||
|
||||
|
@ -16,6 +16,8 @@ We'll add defaults to those in our shortcode parser so they're optional.
|
||||
**mysite/_config.php**
|
||||
|
||||
```php
|
||||
use SilverStripe\View\Parsers\ShortcodeParser;
|
||||
|
||||
ShortcodeParser::get('default')->register('googlemap', function($arguments, $address, $parser, $shortcode) {
|
||||
$iframeUrl = sprintf(
|
||||
'http://maps.google.com/maps?q=%s&hnear=%s&ie=UTF8&hq=&t=m&z=14&output=embed',
|
||||
|
@ -58,7 +58,7 @@ explicitly logging in or by invoking the "remember me" functionality.
|
||||
Now you just need to apply this extension through your config:
|
||||
|
||||
```yml
|
||||
Member:
|
||||
SilverStripe\Security\Member:
|
||||
extensions:
|
||||
- MyMemberExtension
|
||||
|
||||
|
@ -16,6 +16,8 @@ To include your fixture file in your tests, you should define it as your `$fixtu
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
|
||||
class MyNewTest extends SapphireTest
|
||||
{
|
||||
|
||||
@ -31,6 +33,8 @@ You can also use an array of fixture files, if you want to use parts of multiple
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
|
||||
class MyNewTest extends SapphireTest
|
||||
{
|
||||
|
||||
@ -293,6 +297,8 @@ using them.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$factory = Injector::inst()->create('FixtureFactory');
|
||||
|
||||
$obj = $factory->createObject('Team', 'hurricanes');
|
||||
|
@ -11,6 +11,8 @@ with information that we need.
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
class MyObjectTest extends SapphireTest
|
||||
{
|
||||
|
@ -8,6 +8,8 @@ email was sent using this method.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\Email\Email;
|
||||
|
||||
public function MyMethod()
|
||||
{
|
||||
$e = new Email();
|
||||
|
@ -69,6 +69,8 @@ You can check for the current environment type in [config files](../configuratio
|
||||
Checking for what environment you're running in can also be done in PHP. Your application code may disable or enable
|
||||
certain functionality depending on the environment type.
|
||||
```php
|
||||
use SilverStripe\Control\Director;
|
||||
|
||||
if (Director::isLive()) {
|
||||
// is in live
|
||||
} elseif (Director::isTest()) {
|
||||
|
@ -14,6 +14,8 @@ For informational and debug logs, you can use the Logger directly. The Logger is
|
||||
can be accessed via the `Injector`:
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
Injector::inst()->get(LoggerInterface::class)->info('User has logged in: ID #' . Security::getCurrentUser()->ID);
|
||||
Injector::inst()->get(LoggerInterface::class)->debug('Query executed: ' . $sql);
|
||||
```
|
||||
|
@ -15,6 +15,6 @@ block of html on your page.
|
||||
Only:
|
||||
environment: 'dev'
|
||||
---
|
||||
SSViewer:
|
||||
SilverStripe\View\SSViewer:
|
||||
source_file_comments: true
|
||||
```
|
@ -18,6 +18,9 @@ bottle-necks and identify slow moving parts of your application chain.
|
||||
The [Debug](api:SilverStripe\Dev\Debug) class contains a number of static utility methods for more advanced debugging.
|
||||
|
||||
```php
|
||||
use SilverStripe\Dev\Debug;
|
||||
use SilverStripe\Dev\Backtrace;
|
||||
|
||||
Debug::show($myVariable);
|
||||
// similar to print_r($myVariable) but shows it in a more useful format.
|
||||
|
||||
|
@ -47,6 +47,8 @@ This factory allows us you to globally define an adapter for all cache instances
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
```
|
||||
|
||||
@ -65,7 +67,9 @@ Cache objects follow the [PSR-16](http://www.php-fig.org/psr/psr-16/) class inte
|
||||
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
|
||||
|
||||
@ -89,7 +93,9 @@ this will only affect a subset of cache keys ("myCache" in this example):
|
||||
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
|
||||
// remove all items in this (namespaced) cache
|
||||
@ -101,7 +107,9 @@ You can also delete a single item based on it's cache key:
|
||||
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
|
||||
// remove the cache item
|
||||
@ -112,7 +120,9 @@ Individual cache items can define a lifetime, after which the cached value is ma
|
||||
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
|
||||
// remove the cache item
|
||||
@ -144,7 +154,9 @@ old cache keys will be garbage collected as the cache fills up.
|
||||
|
||||
|
||||
```php
|
||||
use Psr\SimpleCache\CacheInterface
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$cache = Injector::inst()->get(CacheInterface::class . '.myCache');
|
||||
|
||||
// Automatically changes when any group is edited
|
||||
|
@ -18,6 +18,8 @@ headers:
|
||||
|
||||
### HTTP::set_cache_age
|
||||
```php
|
||||
use SilverStripe\Control\HTTP;
|
||||
|
||||
HTTP::set_cache_age(0);
|
||||
```
|
||||
|
||||
@ -46,7 +48,7 @@ Cookie, X-Forwarded-Protocol, User-Agent, Accept
|
||||
To change the value of the `Vary` header, you can change this value by specifying the header in configuration
|
||||
|
||||
```yml
|
||||
HTTP:
|
||||
SilverStripe\Control\HTTP:
|
||||
vary: ""
|
||||
```
|
||||
|
||||
|
@ -24,6 +24,8 @@ SilverStripe can request more resources through `Environment::increaseMemoryLimi
|
||||
</div>
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Environment;
|
||||
|
||||
public function myBigFunction()
|
||||
{
|
||||
Environment::increaseTimeLimitTo(400);
|
||||
|
@ -15,6 +15,8 @@ The [api:Security] class comes with a static method for getting information abou
|
||||
Retrieves the current logged in member. Returns *null* if user is not logged in, otherwise, the Member object is returned.
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Security;
|
||||
|
||||
if( $member = Security::getCurrentUser() ) {
|
||||
// Work with $member
|
||||
} else {
|
||||
@ -32,6 +34,8 @@ This is the least desirable way of extending the [Member](api:SilverStripe\Secur
|
||||
You can define subclasses of [Member](api:SilverStripe\Security\Member) to add extra fields or functionality to the built-in membership system.
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Member;
|
||||
|
||||
class MyMember extends Member {
|
||||
private static $db = array(
|
||||
"Age" => "Int",
|
||||
@ -58,6 +62,8 @@ details in the newsletter system. This function returns a [FieldList](api:Silve
|
||||
parent::getCMSFields() and manipulate the [FieldList](api:SilverStripe\Forms\FieldList) from there.
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\TextField;
|
||||
|
||||
public function getCMSFields() {
|
||||
$fields = parent::getCMSFields();
|
||||
$fields->insertBefore("HTMLEmail", new TextField("Age"));
|
||||
|
@ -26,6 +26,10 @@ come from user input.
|
||||
|
||||
Example:
|
||||
```php
|
||||
use SilverStripe\ORM\DB;
|
||||
use SilverStripe\ORM\DataObject;
|
||||
use SilverStripe\ORM\Queries\SQLSelect;
|
||||
|
||||
$records = DB::prepared_query('SELECT * FROM "MyClass" WHERE "ID" = ?', [3]);
|
||||
$records = MyClass::get()->where(['"ID" = ?' => 3]);
|
||||
$records = MyClass::get()->where(['"ID"' => 3]);
|
||||
@ -40,6 +44,9 @@ Parameterised updates and inserts are also supported, but the syntax is a little
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\Queries\SQLInsert;
|
||||
use SilverStripe\ORM\DB;
|
||||
|
||||
SQLInsert::create('"MyClass"')
|
||||
->assign('"Name"', 'Daniel')
|
||||
->addAssignments([
|
||||
@ -80,6 +87,8 @@ handled via prepared statements.
|
||||
|
||||
Example:
|
||||
```php
|
||||
use SilverStripe\Security\Member;
|
||||
|
||||
// automatically escaped/quoted
|
||||
$members = Member::get()->filter('Name', $_GET['name']);
|
||||
// automatically escaped/quoted
|
||||
@ -555,6 +564,9 @@ a [PasswordValidator](api:SilverStripe\Security\PasswordValidator):
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Security\Member;
|
||||
use SilverStripe\Security\PasswordValidator;
|
||||
|
||||
$validator = new PasswordValidator();
|
||||
$validator->minLength(7);
|
||||
$validator->checkHistoricalPasswords(6);
|
||||
@ -672,6 +684,8 @@ variable will be no longer necessary, thus it will be necessary to always set
|
||||
SilverStripe recommends the use of TLS(HTTPS) for your application, and you can easily force the use through the
|
||||
director function `forceSSL()`
|
||||
```php
|
||||
use SilverStripe\Control\Director;
|
||||
|
||||
if (!Director::isDev()) {
|
||||
Director::forceSSL();
|
||||
}
|
||||
@ -685,7 +699,7 @@ use a [secure session](https://docs.silverstripe.org/en/3/developer_guides/cooki
|
||||
To do this, you may set the `cookie_secure` parameter to `true` in your `config.yml` for `Session`
|
||||
```yml
|
||||
|
||||
Session:
|
||||
SilverStripe\Control\Session:
|
||||
cookie_secure: true
|
||||
```
|
||||
|
||||
@ -700,6 +714,7 @@ clear text and can be intercepted and stolen by an attacker who is listening on
|
||||
code. It is best practice to set this flag unless the application is known to use JavaScript to access these cookies
|
||||
as this prevents an attacker who achieves cross-site scripting from accessing these cookies.
|
||||
```php
|
||||
use SilverStripe\Control\Cookie;
|
||||
|
||||
Cookie::set('cookie-name', 'chocolate-chip', $expiry = 30, $path = null, $domain = null, $secure = true,
|
||||
$httpOnly = false
|
||||
|
@ -24,6 +24,8 @@ SilverStripe\Core\Injector\Injector:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\Email\Email;
|
||||
|
||||
$email = new Email($from, $to, $subject, $body);
|
||||
$email->sendPlain();
|
||||
```
|
||||
@ -135,6 +137,8 @@ Configuration of those properties looks like the following:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\Director;
|
||||
use SilverStripe\Core\Config\Config;
|
||||
if(Director::isLive()) {
|
||||
Config::inst()->update('Email', 'bcc_all_emails_to', "client@example.com");
|
||||
} else {
|
||||
|
@ -36,6 +36,8 @@ The loader would be triggered through the `load()` method:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Dev\CsvBulkLoader;
|
||||
|
||||
$loader = new CsvBulkLoader('Member');
|
||||
$result = $loader->load('<my-file-path>');
|
||||
```
|
||||
|
@ -24,6 +24,8 @@ An outline of step one looks like:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\RSS\RSSFeed;
|
||||
|
||||
$feed = new RSSFeed(
|
||||
$list,
|
||||
$link,
|
||||
@ -201,6 +203,8 @@ Say from that last example we want to include the Players Team in the XML feed w
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\RSS\RSSFeed;
|
||||
|
||||
public function players()
|
||||
{
|
||||
$rss = new RSSFeed(
|
||||
|
@ -1,64 +0,0 @@
|
||||
title: Embed an RSS Feed
|
||||
|
||||
# Embed an RSS Feed
|
||||
|
||||
[RestfulService](api:RestfulService) can be used to easily embed an RSS feed from a site. In this How to we'll embed the latest
|
||||
weather information from the Yahoo Weather API.
|
||||
|
||||
First, we write the code to query the API feed.
|
||||
|
||||
**mysite/code/Page.php**
|
||||
|
||||
|
||||
```php
|
||||
public function getWellingtonWeather()
|
||||
{
|
||||
$fetch = new RestfulService(
|
||||
'https://query.yahooapis.com/v1/public/yql'
|
||||
);
|
||||
|
||||
$fetch->setQueryString([
|
||||
'q' => 'select * from weather.forecast where woeid in (select woeid from geo.places(1) where text="Wellington, NZ")'
|
||||
]);
|
||||
|
||||
// perform the query
|
||||
$conn = $fetch->request();
|
||||
|
||||
// parse the XML body
|
||||
$msgs = $fetch->getValues($conn->getBody(), "results");
|
||||
|
||||
// generate an object our templates can read
|
||||
$output = new ArrayList();
|
||||
|
||||
if($msgs) {
|
||||
foreach($msgs as $msg) {
|
||||
$output->push(new ArrayData([
|
||||
'Description' => Convert::xml2raw($msg->channel_item_description)
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
This will provide our `Page` template with a new `WellingtonWeather` variable (an [ArrayList](api:SilverStripe\ORM\ArrayList)). Each item has a
|
||||
single field `Description`.
|
||||
|
||||
**mysite/templates/Page.ss**
|
||||
|
||||
|
||||
```ss
|
||||
|
||||
<% if WellingtonWeather %>
|
||||
<% loop WellingtonWeather %>
|
||||
$Description
|
||||
<% end_loop %>
|
||||
<% end_if %>
|
||||
```
|
||||
|
||||
## Related
|
||||
|
||||
* [RestfulService Documentation](../restfulservice)
|
||||
* [RestfulService](api:RestfulService)
|
@ -137,6 +137,8 @@ in order to read page limit information. It is also passed the current
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\ORM\PaginatedList;
|
||||
|
||||
public function getResults($searchCriteria = [])
|
||||
{
|
||||
$start = ($this->getRequest()->getVar('start')) ? (int)$this->getRequest()->getVar('start') : 0;
|
||||
|
@ -29,6 +29,8 @@ you want to set.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\i18n\i18n;
|
||||
|
||||
// mysite/_config.php
|
||||
i18n::set_locale('de_DE'); // Setting the locale to German (Germany)
|
||||
i18n::set_locale('ca_AD'); // Setting to Catalan (Andorra)
|
||||
@ -82,6 +84,8 @@ You can use these settings for your own view logic.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Config\Config;
|
||||
|
||||
Config::inst()->update('i18n', 'date_format', 'dd.MM.yyyy');
|
||||
Config::inst()->update('i18n', 'time_format', 'HH:mm');
|
||||
```
|
||||
@ -107,7 +111,7 @@ In order to add a value, add the following to your `config.yml`:
|
||||
|
||||
```yml
|
||||
|
||||
i18n:
|
||||
SilverStripe\i18n\i18n:
|
||||
common_locales:
|
||||
de_CGN:
|
||||
name: German (Cologne)
|
||||
@ -119,7 +123,7 @@ Similarly, to change an existing language label, you can overwrite one of these
|
||||
|
||||
```yml
|
||||
|
||||
i18n:
|
||||
SilverStripe\i18n\i18n:
|
||||
common_locales:
|
||||
en_NZ:
|
||||
native: Niu Zillund
|
||||
@ -155,6 +159,8 @@ followed by `setLocale()` or `setDateFormat()`/`setTimeFormat()`.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\DateField;
|
||||
|
||||
$field = new DateField();
|
||||
$field->setLocale('de_AT'); // set Austrian/German locale, defaulting format to dd.MM.y
|
||||
$field->setDateFormat('d.M.y'); // set a more specific date format (single digit day/month)
|
||||
@ -341,7 +347,7 @@ To create a custom module order, you need to specify a config fragment that inse
|
||||
Name: customi18n
|
||||
Before: 'defaulti18n'
|
||||
---
|
||||
i18n:
|
||||
SilverStripe\i18n\i18n:
|
||||
module_priority:
|
||||
- module1
|
||||
- module2
|
||||
@ -402,6 +408,8 @@ If using this on the frontend, it's also necessary to include the stand-alone i1
|
||||
js file.
|
||||
|
||||
```php
|
||||
use SilverStripe\View\Requirements;
|
||||
|
||||
Requirements::javascript('silverstripe/admin:client/dist/js/i18n.js');
|
||||
Requirements::add_i18n_javascript('<my-module-dir>/javascript/lang');
|
||||
```
|
||||
|
@ -130,6 +130,8 @@ to live until a publish is made (either on this object, or cascading from a pare
|
||||
When files are renamed using the ORM, all file variants are automatically renamed at the same time.
|
||||
|
||||
```php
|
||||
use SilverStripe\Assets\File;
|
||||
|
||||
$file = File::get()->filter('Name', 'oldname.jpg')->first();
|
||||
if ($file) {
|
||||
// The below will move 'oldname.jpg' and 'oldname__variant.jpg'
|
||||
|
@ -190,6 +190,8 @@ will be moved to `assets/a870de278b/NewCompanyLogo.gif`, and will be served dire
|
||||
the web server, bypassing the need for additional PHP requests.
|
||||
|
||||
```php
|
||||
use SilverStripe\Assets\Storage\AssetStore;
|
||||
|
||||
$store = singleton(AssetStore::class);
|
||||
$store->publish('NewCompanyLogo.gif', 'a870de278b475cb75f5d9f451439b2d378e13af1');
|
||||
```
|
||||
|
@ -62,7 +62,7 @@ The CMS interface can be accessed by default through the `admin/` URL. You can c
|
||||
After:
|
||||
- '#adminroutes'
|
||||
---
|
||||
Director:
|
||||
SilverStripe\Control\Director:
|
||||
rules:
|
||||
'admin': ''
|
||||
'newAdmin': 'AdminRootController'
|
||||
@ -75,7 +75,7 @@ In PHP you should use:
|
||||
|
||||
|
||||
```php
|
||||
AdminRootController::admin_url()
|
||||
SilverStripe\Admin\AdminRootController::admin_url()
|
||||
```
|
||||
|
||||
When writing templates use:
|
||||
|
@ -82,7 +82,7 @@ to the `LeftAndMain.extra_requirements_javascript` [configuration value](../conf
|
||||
|
||||
```yml
|
||||
|
||||
LeftAndMain:
|
||||
SilverStripe\Admin\LeftAndMain:
|
||||
extra_requirements_javascript:
|
||||
- mysite/javascript/MyLeftAndMain.Preview.js
|
||||
```
|
||||
|
@ -8,6 +8,8 @@ SilverStripe lets you customise the style of content in the CMS. This is done by
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\HTMLEditor\HtmlEditorConfig;
|
||||
|
||||
HtmlEditorConfig::get('cms')->setOption('content_css', project() . '/css/editor.css');
|
||||
```
|
||||
|
||||
|
@ -419,6 +419,9 @@ PHP:
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\HTTPResponse;
|
||||
use SilverStripe\View\ViewableData;
|
||||
|
||||
class MyController
|
||||
{
|
||||
public function autocomplete($request)
|
||||
|
@ -11,6 +11,8 @@ at the last position within the field, and expects unescaped HTML content.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Forms\TextField;
|
||||
|
||||
TextField::create('MyText', 'My Text Label')
|
||||
->setDescription('More <strong>detailed</strong> help');
|
||||
```
|
||||
|
@ -78,7 +78,7 @@ or across page types with common characteristics.
|
||||
Now you just need to enable the extension in your [configuration file](../../configuration).
|
||||
```yml
|
||||
// mysite/_config/config.yml
|
||||
LeftAndMain:
|
||||
SilverStripe\Admin\LeftAndMain:
|
||||
extensions:
|
||||
- NewsPageHolderCMSMainExtension
|
||||
```
|
||||
|
@ -67,7 +67,7 @@ Load the new CSS file into the CMS, by setting the `LeftAndMain.extra_requiremen
|
||||
|
||||
```yml
|
||||
|
||||
LeftAndMain:
|
||||
SilverStripe\Admin\LeftAndMain:
|
||||
extra_requirements_css:
|
||||
- mysite/css/BookmarkedPages.css
|
||||
```
|
||||
@ -106,7 +106,7 @@ Enable the extension in your [configuration file](../../configuration)
|
||||
|
||||
```yml
|
||||
|
||||
SiteTree:
|
||||
SilverStripe\CMS\Model\SiteTree:
|
||||
extensions:
|
||||
- BookmarkedPageExtension
|
||||
```
|
||||
@ -143,7 +143,7 @@ Enable the extension in your [configuration file](../../configuration)
|
||||
|
||||
```yml
|
||||
|
||||
LeftAndMain:
|
||||
SilverStripe\Admin\LeftAndMain:
|
||||
extensions:
|
||||
- BookmarkedPagesLeftAndMainExtension
|
||||
```
|
||||
@ -268,7 +268,7 @@ The extension then needs to be registered:
|
||||
|
||||
```yaml
|
||||
|
||||
LeftAndMain:
|
||||
SilverStripe\Admin\LeftAndMain:
|
||||
extensions:
|
||||
- CustomActionsExtension
|
||||
```
|
||||
|
@ -10,6 +10,9 @@ This can be accessed in user code via Injector
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Kernel;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
|
||||
$kernel = Injector::inst()->get(Kernel::class);
|
||||
echo "Current environment: " . $kernel->getEnvironment();
|
||||
```
|
||||
|
@ -15,6 +15,8 @@ Sets the value of cookie with configuration.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\Cookie;
|
||||
|
||||
Cookie::set($name, $value, $expiry = 90, $path = null, $domain = null, $secure = false, $httpOnly = false);
|
||||
|
||||
// Cookie::set('MyApplicationPreference', 'Yes');
|
||||
@ -54,6 +56,10 @@ from the browser.
|
||||
|
||||
|
||||
```php
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Control\Cookie;
|
||||
use SilverStripe\Control\CookieJar;
|
||||
|
||||
$myCookies = [
|
||||
'cookie1' => 'value1',
|
||||
];
|
||||
@ -102,7 +108,7 @@ If you need to implement your own Cookie_Backend you can use the injector system
|
||||
Name: mycookie
|
||||
After: '#cookie'
|
||||
---
|
||||
Injector:
|
||||
SilverStripe\Core\Injector\Injector:
|
||||
Cookie_Backend:
|
||||
class: MyCookieJar
|
||||
```
|
||||
|
@ -29,6 +29,8 @@ class MyController extends Controller
|
||||
Otherwise, if you're not in a controller, get the request as a service.
|
||||
|
||||
```php
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
|
||||
$request = Injector::inst()->get(HTTPRequest::class);
|
||||
$session = $request->getSession();
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user