CakePHP 3: Inflector class not found - cakephp-3.0

In one of my "Elements" I'd like to use the Inflector class, but it always shows
Fatal error: Inflector class not found.
How do I include this class in cake way in my view files?
I tried to add use Cake\Utility and use Cake\Utility\Inflector, but neither of them helped.

Try this one
\Cake\Utility\Inflector::humanize('Inflector working in the view')
fully qualified namespace from anywhere like Model, View or Controller
Note: Do no forget to use "\" at the start!

You can just fully qualify the inflector class methods i.e. Cake\Utility\Inflector::method() e.g.
<?= Cake\Utility\Inflector::humanize('Inflector working in the view') ?>
OR
You can just create a custom helper class, use the Inflector class there and then call the custom helper method in your view.

Related

Using ArrayHelper in view file in Yii2

I am trying to use ArrayHelper in Yii2 advanced template project and it says Class ArrayHelper not found. I added use yii\helpers\ArrayHelper and it works in controller. But this problem continue to happen when I use ArrayHelper in view file. How can I autoload these classes in Yii2.
make sure you add namespace in view file too.
//in view_file_name.php
use yii\helpers\ArrayHelper;
You Did not show the code . So i am not able to give you the exact answer:
But you ask that you want to use array helper in your view so try this:
it is working .
use app\models\User;
use yii\helpers\ArrayHelper;
//use app\models\user;
$user=User::find()->all();
//use yii\helpers\ArrayHelper;
$listData=ArrayHelper::map($user,'user_id','username');
echo '<pre>';
print_r($listData);
echo '</pre>';
for more details visit :Yii2 Docs

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 parse a Razor template with partials from a custom folder?

I have two cshtml-files in the same subfolder of Views. One of the templates is meant to include the other template. I tried to accomplish that as follows:
Main template:
<html>
<head></head>
<body>
#Html.Partial("~/Views/Pdfs/Header");
</body>
</html>
The error I get is
Unable to compile template. The name 'Html' does not exist in current context.
What am I supposed to do additionally?
As commented by Erik there is no Html in RazorEngine (see the linked answer), however you can use #Include("mytemplate") instead.
If you want to be compatible with the #Html.Partial() syntax for some reason you can extend the RazorEngine syntax like this.
Basically what you want to do is provide your own class inheriting from TemplateBase<T> (or ITemplate to be exact) and then set it either via configuration or the #Inherit MyBaseClass<MyModel> syntax. In this case you could just call the Include method from your Partial method within the Html helper class.
Been annoyed by this for a long time. Wrote all the infrastructure classes to just get this working like you'd expect in MVC, without all the MVC burden:
var razor = RazorHelper.O;
var html = razor.RenderFromMvc(#"Views\RazorEngine\TestEmail.cshtml", vm);
https://github.com/b9chris/RazorEngineComplete

TYPO3: Write a ViewHelper in an extension with vendor namespace

I have created an extension with plugins in a vendor namespace. Everything works fine so far. All the classes start with a namespace declaration namespace \VENDOR\ExtensionName\... and typo3 autoloads them just fine.
However, the problem starts when I add a ViewHelper. I put this in the \VENDOR\ExtensionName\ViewHelpers namespace. I added the namespace in my Fluid template like so: {namespace ns=VENDOR\ExtensionName\ViewHelpers}. When I call it in the template with <ns:myViewHelper />, I just get an 'Oops ...' error message:
Could not analyse class:VENDOR\ExtensionName\ViewHelpers\MyViewHelperViewHelper maybe not loaded or no autoloader?
The same happens, when I put the ViewHelper in a \TYPO3\... namespace.
How do I correctly implement a ViewHelper in an extension with a vendor namespace?
The namespace declaration must not have a leading backslash.
Instead having \Vendor\ExtensionName\ViewHelpers; it has to be Vendor\ExtensionName\ViewHelpers;
http://www.php.net/manual/en/language.namespaces.nested.php

Alternative for getDefinitionByName

In the past, there was a simple trick, to include the flex mxmlc module by adding the following line to the flash-cs4 makefile:
-include-libraries “/absolute/path/to/my/assets/assets.swc”
This gave you the ability to use getDefinitionByName, an helper function to access the embedded swc asset-library (instead of creating hand-written classes all assets).
Unfortunately, this has stopped working since flash-cs4. Does anybody know another solution?
Unfortunately, the only workaround I have found is to explicitly refer each of asset classes somewhere in the code. You may create dummy class like this:
public class AssetsExporter extends Sprite
{
public static function export()
{
AssetClass1;
AssetClass2;
//etc
trace( "DEBUG AssetsExporter.export()" );
}
}
Specify this class as Document class in Flash IDE, so it will be compiled into the resulting swc. Then in the main code of your application call
AssetsExporter.export();
After doing this you will be able to use getDefinitionByName().
You can add them to the libraries in the publish settings.
(Image from http://wiki.gigya.com/ via Google Images)
By the way, if you use SWC files, you can also do
new somepackage.SomeClass();
instead of
new getDefinitionByName("somepackage.SomeClass")
where applicable. This is because SWC files are included at compile time, not runtime.
Even though you can change a compiler setting manually, its easy if you are using FlashDevelop as this is very simple to fix.
Right click your included swc from the Project list. Choose options then "include library (complete library)".
..you can now use getDefinitionByName to get an unreferenced class from your swc file.