CakePHP 3.x how to point the templates to a file inside a Plugin? - cakephp

According to http://book.cakephp.org/3.0/en/views/helpers/form.html#customizing-the-templates-formhelper-uses,
the way to point to a list of template containers, is to create a file inside config folder.
// In a View class
$this->loadHelper('Form', [
'templates' => 'app_form',
]);
This would load the tags in config/app_form.php. This file should
contain an array of templates indexed by name:
How do I do the same by pointing at a file inside a Plugin instead?

How do I do the same by pointing at a file inside a Plugin instead?
Guess as everywhere else in the framework? Dot notation: PluginName.app_form
If that is not working it should be added to the framework to be consistent.

Related

Add javascript file at the end of the script block

i am using cakephp 3.7 and i load few .js file in my default.ctp layout. The problems comes when i try to add other .js in my view. These js files are added at the beginning of the js file list.
I do echo $this->fetch('script'); to print that block.
For example, i am using jquery, i load this library in default.ctp because i use it everywhere, the problem is that view js are loaded before jquery so i cannot use $
how can i "append" to script block, inside an action's view?
Thanks
In your layout put (before < /head> if you wish)
$this->fetch('my_head_script')
, then in your view use
$this->Html->script("jquery.js",['block' => 'my_head_script']);
Did You use script blocks, described here: https://book.cakephp.org/3.0/en/views/helpers/html.html#creating-inline-javascript-blocks?

App::build in CakePHP 3

Firstly, I'm very new in cakephp. In version 2.x, it allowed App::build to specify Controllers in specified folder. For example:
App::build(array(
'Controller' => array(
ROOT . '/app/Controller/Api/'
)
));
But in cakephp3.x, App::build is no more available. So how can I make a same thing in cakephp3.x ?
As written in the cakephp doc here : http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#configuration, the App::build is not a part of cakephp3 anymore.
So, you'll have to make a specific configuration for the cakephp autoloader (use composer):
"autoload": {
"psr-4": {
"App\\Controller\\": "/path/to/directory/with/controller/folders"
}
}
More information about this config : http://book.cakephp.org/3.0/en/development/configuration.html#additional-class-paths
More information about composers autoloader :
https://getcomposer.org/doc/01-basic-usage.md#autoloading
App::build has been removed but what you want can be done with prefix routing in Cake3. This is exactly what you try to solve. Taken from the documentation:
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having prefixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Using our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src/Template/Admin/Users/edit.ctp
Just replace admin with api from the example and read the whole section of the manual I've linked and you're done.

Loading partial html template into Angular app using Laravel

I'm trying to use the RouteProvider functionality in Angular. A different partial html template should be loaded depending on whether the user is editing a form, viewing a list of completed entries, etc. I've been unable to load the html templates within the same page. Instead, the user is redirected to a different page.
Here's the relevant Angular code:
.when(/new', {
controller: 'CreateCtrl'
templateUrl: 'partials/newform.html'
The Laravel Route:
Route::resource('services', 'ServicesController');
The newform.html file is located at resources/views/partials/newform.html within Laravel.
Any thoughts on how I can load these partial html templates from Laravel?
One way would be to reference the full path to the partials
.when('/new', {
controller: 'CreateCtrl'
//depending on your path adjust it
templateUrl: 'partials/newform'
since you are just using .html not tempalte.blade.php file for template you should move it to public folder.
Update:
If you really dont want to move the template out of view folder of laravel
create a route and serve your html from there
Route::group(array('prefix' => 'partials'), function(){
Route::get('/newform', function()
{
return File::get(app_path().'Views/partials/angular.html');
});
});
Note: I will suggest you not to mix Laravelc template with Angular, keep Laravel as REST API, and your Angular should separate layer.
Here are some Pros and Cons for both approach
I found another way using gulp, i leave my solution :)
In gulpfile.js inside elixir function add this line:
var elixir = require('laravel-elixir');
elixir(function(mix) {
mix.copy('resources/assets/js/angular/components/**/*.template.html', public/angular-templates');
//Find all files with suffix .template.html
});
As you notice it, i created a folder called 'angular' and then another one called 'components', there we will have our components
Angular-----------------------------
--Components------------------------
----my-component.directive.js-------
----my-component.template.html------
We have to create a global angular variable taking our browser window origin (www.myapp.com, localhost:8000, etc) by doing:
angular.module('myModule',[])
.value('pathAssets', location.origin + '/angular-templates/')
In our templateUrl we will call the template by writting:
templateUrl: pathAssets + '../angular-templates/my-template.html',
I have to say we have to concat our angular files in a file, otherwise it won't work D: if you don't know how to do it, add these lines in your gulpfile.js
mix.scripts([
'../../../bower_components/angular/angular.min.js',
'angular/app.js',
'angular/controllers/**/*.controller.js',
'angular/components/**/*.directive.js',
'angular/bootstrap.js',
], 'public/js/app.js'); //We concatenate angular files saving them in app.js
Finally execute the command 'gulp' in terminal(In our project), it should generate a new folder in public called angular-templates.
CONCLUSION
1. Move all your angular templates to public inside a specific folder by using elixir.
2. create a global variable in angular to save your origin URL and use it in your directives it automatically with this template in public.

Load files that aren't within the Elements or View folders?

Hi I'm trying to make something like CMS with widgets so I have a folder widgets in my app folder there are my widgets for example app/Widgets/UsersOnline/UserOnline.php
I loading them from db with widgets model in beforefilter method in appController and there i pass them to the view, so with a widget helper I would like to render them on the position. But i can't get to the folder widgets/UserOnline/view.ctp with view->element() method this method require file to be in Elements/.
TLDR / Actual Question:
Is there any way to load files in view outside the view/ and /elements ? Thanks in advance.
You can use relative paths when calling the element:
<?php echo $this->Element('../../Widgets/UsersOnline/UserOnline'); ?>
Don't forget to name your element file 'UserOnline.ctp' too.

Linking a CSS or JS to default layout

I'm trying to undestand how to link CSS or JS I'll use it all over my CakePHP 1.3 application. I've read about putting assets in folder /app/webroot/css or /app/layouts/css (for css only in this case).
I've put a file named main.css and default.css but I'm missing something.
How can I do to fix it and which are the default rules for the default layout?
Normal
$this->Html->css('my_file'); corresponds to /app/webroot/css/my_file.css
$this->Html->script('my_file'); corresponds to /app/webroot/js/my_file.js
you add the php part from above to your layout file which is by default in /app/views/layouts/default.ctp (or in cake dir if you have not created one)
you can set the layout in the controller/app_controller setting $this->layout = 'foo'; which points to /app/views/layouts/foo.ctp
Themes
Setting the controller to $this->view = 'Theme'; will make cake use themes then setting $this->theme = 'SomeTheme'; in the controller will make cake use /app/views/themed/some_theme/* files
using $this->Html->script('my_file'); now points to /app/views/themed/some_theme/js/my_file.js and the same goes for the css.
css = http://book.cakephp.org/view/1437/css
js = http://book.cakephp.org/view/1589/script
themes = http://book.cakephp.org/view/1093/Themes
themes have the problem of serving css, js and other assets through php (ob_start(); include etc) and this is obviously slower than normal http serving. you can either copy files across to the webroot folder as explained in the bottom or be lazy and do something like the following https://gist.github.com/712622

Resources