cake 2.3.x create menu - cakephp

I need to help with add menu to my apps.
I use cake 2.3.8. I tried to use MenuHelper from this article http://bakery.cakephp.org/articles/alkemann/2009/02/04/menuhelper.
I add this class to lib>Cake>View as MenuHelper.php
Next I added from AppsController $helpers =>array('Menu');
But I don't know what I should to do next. I tried use
$menu->add('link_list','title','url');
in my view or layout but cake doesn't view $menu variable. What I do wrong?

You're using the CakePHP 1.x syntax in your snippet, hence it doesn't work. In CakePHP 2.x, helpers are exposed as a property in the view and you have to use $this->Menu->add('link_list','title','url');.

Related

Include utility class all over the application cakephp 3

I have custom utility class that contains some methods for general use and it is located in src/Utility/UtilityClass.
I can include the Utility class in controller .e.g.
use App\Utility\ArrayUtil;
and then call the class in my controllers e.g.
ArrayUtil::myMethod();
And it works. I need to include the UtilityClass in bootstrap so it applies all over the application so I can reuse it in models, templates and other controllers as well.
I tried to load it in config/bootstrap.php but I get this error:
Error: Class 'ArrayUtil' not found
Any idea
You can add this line at the top of the page, be it Model, view or controller.
use App\Utility\ArrayUtil;
If you're using this utility in multiple views, then I'd suggest you to write this line in Template/Layout/default.ctp, since all templates would be a part of this.
Hope this helps.
Peace! xD

Manipulating convention of cakePHP

i have a controller PostmustController which doesn't have any models naming Postmust. it will load a model named Post how can i do it in cakePHP.
Learned and solved it. And there is a anotherway.
You can also use this
Controller::loadModel('Post');
You can learn the detail from the following link.
Uses following array in your controller it will automatically load models in your controller.
public $uses = array('Post');

render a blank view in cakephp

i need to prevent a view to be rendered in a specified case but i can't understand how to prevent it to render.
I tried
$this->autoRender=false
but nothing happened, probably because i'm using an API engine that manage rendering differently from regular controllers. Anyone know any trick to do this?
Using $this->layout = 'ajax' does not seem to be enough.
But using these both lines works:
$this->layout = 'ajax';
$this->render(false);
While searching for a solution, I found this answer. Now when using CakePHP 2.4.x, you could use the following code in your controller:
$this->layout = false;
This will lead to just the view being rendered, without a layout.
It's an old question. The current cake-version is 3.x and there is a easy way to use a blank layout.
Only add the in the controller:
$this->viewBuilder()->autoLayout(false);
Try to use ajax layout $this->layout = 'ajax' this is the default empty layout, which is used for ajax methods.
public function function_without_layout(){
$this->viewBuilder()->autoLayout(false);
echo "hello Brij";
exit;
}
$this->layout = false; is deprecated in CakePHP version 3.
Use $this->viewBuilder()->autoLayout(false); for CakePHP version 3.
Add this in your controller:
$this->autoRender = false;
This works in my project.
The CakePHP 3 autoLayout(false) method from the other answer will still have the system try to locate a corresponding view/template file for the action you're calling. Since I needed no output at all, this didn't work for me, so I needed to also render an empty template.
Creating a blank .ctp file for every empty action you might need isn't an option really, because you'd normally want to have one and reuse it. CakePHP 2 had a $this->viewPath property which would let you configure the controller to look into the app/View folder, but it's CakePHP 3 alternative still looks into the corresponding controller and prefix folders. There is a not-so-obvious way to force CakePHP3 to look for a template in a root view path.
Create src/Template/my_blank_view.ctp
Add the following to your controller action:
$this->viewBuilder()->layout(false);
$this->viewBuilder()->templatePath('.'); // this
$this->viewBuilder()->template('my_blank_view');
Also, I'm using $this->viewBuilder()->layout(false) instead of autoLayout(false) because the latter kind of implies that there might be another layout set later, where the layout(false) just explicitly sets that there's no layout needed.
without knowing anything about API engine you're using, maybe try to make empty layout with empty content and call it in controller as $this->layout = 'empty_layout'

CakePHP: requestAction and problems with styles / scripts

I have one problem with CakePHP and $requestAction. In the 1.3 manual of cakephp there is written
It is important to note that making a requestAction using 'return' from a controller method can cause script and css tags to not work correctly.
Now I got this case. The contents of $html->css() and $html->script() are not displayed in the final view after using a $this->requestAction() in combination with return in the controller.
Does anybody know a workaround for this problem? How to use styles and scripts together with $this->requestAction(<...>, array('return'))?

Simple examples on how to use CakePHP pages_controller.php to create static pages

I am complete beginner to CakePHP but I am a bit knowledgeable in ROR.
Can somebody pls give me some simple examples on how to make use of pages_controller.php? I want to create static pages such as Home, About, and etc but I don't know how and where to start. I tried something like creating a about.ctp in the views and creating about_controller.php (this is how being done in Ruby on Rails) but I just got some errors.
I concluded that all of the static pages will only use 1 controller which is pages_controller.php but I dont know how.
I tried reading the article found on this link:
http://book.cakephp.org/view/958/The-Pages-Controller
but it doesn't give me anything that will help me learn how to use it.
This is what I got from the page:
CakePHP core ships with a default controller called the Pages Controller (cake/libs/controller/pages_controller.php). The home page you see after installation is generated using this controller. It is generally used to serve static pages. Eg. If you make a view file app/views/pages/about_us.ctp you can access it using url http://example.com/pages/about_us
When you "bake" an app using CakePHP's console utility the pages controller is copied to your app/controllers/ folder and you can modify it to your needs if required. Or you could just copy the pages_controller.php from core to your app.
Can somebody pls show me or explain to me how??? I am a total beginner pls help.
It's pretty self explanatory.
Create a file in your APP/views/pages/ folder - e.g about_us.ctp
Type in your content. No layout; just the text, tables/images/etc
<h3>About my site</h3>
<p>bla bla la</p>
<?php echo $this->Html->image('my_img.jpg'); ?>
Save.
Go to www.site.com/pages/about_us - your page is served.
Pages is the controller to serve static pages. you don't need an about_controller, unless you need something more than just a static page.
You can change how the link looks by using routing.
You can set variables for use in your template as well:
about_us.ctp
<?php
$this->set('title_for_layout', 'My about page');
$this->set('active_link', 'about');
?>
<h1>My page!</h1>
etc

Resources