Global pagination settings for Rest - yii2

How can I set defaultPageSize and pageSizeLimit globally in an Yii2 application? Docs does not seem to have this information, and all solutions on SO are controller-based.
Preferably in main.php

You can use di container
just plop this in your bootstrap file
Yii::$container->set('yii\data\Pagination', ['pageSize' => 150]);

Related

Enqueue styles and scripts for WordPress Gutenberg block

what are the best or the recommended way to Enqueue styles and scripts for WordPress Gutenberg block ?
i see some developers recommend enqueue_block_assets hook and others recommend init hook with refering to styles and scripts handles in register_block_type php function
What should i use ?
It does depends on the use case as not all cases are the same but I believe and stand to be corrected, Wordpress now recommends using register_block_type to add scripts and css instead.
This is because adding it via block registration allows Wordpress to add the css only when a block is used.
Test it and see. Add block assets using enqueue_block_assets and it will load on every page even if the block is not loaded on the page.
The styles and scripts added below will only be added when the block is added to a page.
My answer is based on some of my own testing recently. CSS Added via blocks using the register block are injected when a block is added not at load time like with enqueue_block_assets
Also see this article as a reference in terms of it now being recommended by Wordpress
register_block_type('namespace/your-block', array(
'style' => 'view-style-handle',
'editor_style' => 'editor-style-handle',
'editor_script' => 'block-script-handle'
));

Where to place widgets in Yii2 application

I'm using Yii2 to create my application. I also like to use the widget feature, but I'm not sure, what is the best practice for the widget code location. The Yii2 directory-structure provides 2 directories:
#app/components
#app/widgets
The location "2." (#app/widgets) sounds like the best location for widgets. But, the Yii2 documentation for widgets used the location "1." (#app/components)
So my question: what is the right place for own widgets?
I put it in #app/components/*.
There I also put
#app/components/widgets/*
#app/components/helpers/*
#app/components/validators/*
#app/components/otherComponent/*
#app/components/MyActiveRecord.php
#app/components/MyController.php
and other components of my application

How to make default layout in cakephp3

Actually I work on cakephp3 project.
I want to change the style of the website and put an existing template.
I found a tutorial on how to do that, but I think it was for cakephp2, because I didn't find any file called Dafault.ctp in View folder.
And I tried to put the html code of my template in the home.ctp, but I saw the top-bar navigation which contains Documentation API ...
So where is the Default Layout in cakephp3?
cakephp3 Layouts are in src/Template/Layout/. your view file always use default.ctp file, but you can use other layouts.
// From a controller
public function admin_view()
{
// Set the layout.
$this->viewBuilder()->layout('admin');
// Before 3.1
$this->layout = 'admin';
}
// From a view file
$this->layout = 'loggedin';
a better definition in cakephp3 doc here.
Hi I solved my problem..
I found the Default.ctp
He is in Template->Layout->Default.ctp
contrary to cakephp2 View->Layouts-Default.ctp

Yii2 add google font to head

I was wondering how do you add link tag/google font to head in yii2.
I want to add the following
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,600,700' rel='stylesheet' type='text/css'>
I have found this documentation but doesn't mention anything about link/adding google font etc
The correct answer is to create a new AssetBundle.
While you can directly place the HTML for the fonts into the of your main.php file, this isn't the Yii way. If you have tried to load jQuery files this way, you might notice odd behavior when directly putting them into the HTML.
For example: Directly place the HTML tag for Bootstrap CDN into the head of your main.php. Then, somewhere in your code try to use the tooltip. You will get an error in your console that tooltip is not a function. - This is because the way Yii puts all your template files together, and at that time, Bootstrap is not available.
While simply loading a font probably won't cause any problems, it is a good idea to do things the way they were intended. Following MVC rules, properly documenting your code, and following the Yii best practices, will go a long way. Not only will you thank yourself a year later when you have to go back into a project, but the next guy will appreciate it. I can't stand going into systems, and seeing stuff thrown everywhere, chincy hacks, and spaghetti code, and no documentation or comments.
Correct Way:
Create a new AssetBundle. In your assets folder, you probably already have AppAsset.php. Duplicate it, and name it FontAsset.php.
Here is an example from my project, using 3 Google fonts.
FontAsset.php
<?php
namespace app\assets;
use yii\web\AssetBundle;
class FontAsset extends AssetBundle
{
public $basePath = '#webroot';
public $baseUrl = '#web';
public $css = [
'//fonts.googleapis.com/css?family=Open+Sans:400,700',
'//fonts.googleapis.com/css?family=Ubuntu:400,700',
'//fonts.googleapis.com/css?family=Oswald:400,700'
];
public $cssOptions = [
'type' => 'text/css',
];
}
In your layout, main.php for example. Right under where you see AppAsset::register($this)
main.php
use app\assets\FontAsset;
FontAsset::register($this);
For every layout file that you want to load those fonts, include the FontAsset.
The AssetBundle is basically a bundle of CSS and/or JS files and options. You could add another one for say JWPlayer say named VideoAsset, and add your JS/CSS files for JWPlayer in it.
Point being, you shouldn't be adding these things directly into the HTML of the layouts directly, as it can cause problems. Let the AssetManager handle them by declaring AssetBundles.
It might save you later down the road!
The best way is to create an asset bundle and add the link to the bundle.
You can find a complete guide here:
http://www.yiiframework.com/doc-2.0/guide-structure-assets.html
You can put it directly in the head of the layout (file views/layouts/main.php)

How to serve static pages with Zend Framework

We currently employ Zend Framework and the MVC pattern on our website. We have large numbers of static pages, which are in random areas of the site. These pages are not simple HTML pages that can bypass ZF altogether... they would participate in the Zend_Layout, etc.
What is the best way to serve these pages without creating a separate action/controller for each random page? Redoing the layout of the pages so that they all fall under a "misc" controller or something is not an option, the pages need to stay where they are in our URL hierarchy for SEO purposes.
If I understand the question properly:
You have a bunch of static content you would like to apply your layout to.
These pages of static content already have existing urls you don't want to break
Zend actually separates URL from $controller->action(), it just so happens the MVC part of Zend has a default setting to do this. You can still create a "misc" controller which receives any random url, you just need to define some custom routes.
http://framework.zend.com/manual/en/zend.controller.router.html
quoting example Zend Framework site:
$route = new Zend_Controller_Router_Route_Static(
'login',
array('controller' => 'auth', 'action' => 'login')
);
$router->addRoute('login', $route);
Above route will match a URL of
http://domain.com/login, and dispatch
to AuthController::loginAction().
More ambitious examples using pattern matching can be found on the same page.