add helpers to static pages and layouts - cakephp

Can I add a helper to my static page (for example my homepage) and layouts?
How? (Because no actions are available. In PagesContoller.php, we've display action. I add a home action, but it is overridden by display action)

To use a Helper in every controller and layout you can load in AppController.php:
<?php
class AppController extends Controller {
public $helpers = array('Form', 'Html', 'Js', 'Time', 'MyCustomHelper');
}
?>
Your home action will not work because of the default settings in Config/routes.php:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Any passed argument to /pages/ is sent to the display action. Either create a new route for functions in PagesController or create a new controller.
Example route to get PagesController functions to work:
Router::connect('/pages/show/:action/*', array('controller' => 'pages'));
(Place this route before your /pages/* route!)

Related

CakePHP 3 : link to any action in PagesController opens same action

I have to link to different actions in PagesController.
I have created many static pages and for that I have defined an action like
public function contact(){
}
now when I access www.mysite.com/pages/contact instead of opening contact.ctp it opens the default display action.
The routes.php file contains
Router::defaultRouteClass('DashedRoute');
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
How can I access the static pages or other actions of PagesController?
$routes->connect('/pages/:action/*', ['controller' => 'Pages']);
Now you can call to diferent actions. e.g.
www.mysite.com/pages/contact
www.mysite.com/pages/about
www.mysite.com/pages/someaction
The default routing for the PagesController is to direct everything to the display action.
In order to add additional actions, you would need to route these specifically.
$routes->connect('/pages/contact', ['controller' => 'Pages', 'action' => 'contact']);
Or, alternatively, if you do not want everything to go through the display action, remove the specific line in routes.php that directs everything there. CakePHP would auto-route anything beginning with /pages/ to the PagesController, and anything after the slash to it's appropriate action.
If you make the view file src/Template/Pages/contact.ctp you can access it using the URL http://example.com/pages/contact
No need to change anythin in routes.php they are fine. No need to create a method in your PagesController, this is a simple and optional controller for serving up static content.

Create plugin cakephp and use it in browser

I'm trying to create a plugin, this is my folder layout;
in bootstrap.php
CakePlugin::load('ContactManager');
in routes.php
Router::connect(
'/ct/',
array(
'plugin' => 'ContactManager',
'controller' => 'Contacts',
'action' =>'index'
)
);
ContactManagerAppController.php
<?php
class ContactManagerAppController extends AppController {}
ContactsController.php
<?php
class ContactsController extends ContactManagerAppController {
public $uses = array('ContactManager.Contact');
public function index() {
//...
}
}
ContactManagerAppModel.php
<?php
class ContactManagerAppModel extends AppModel {}
Contact.php
class Contact extends ContactManagerAppModel {}
How do I display index.ctp in my browser
index.ctp
<?php echo 'hello'; ?>
Requesting http://localhost/cakephp/ct/ContactManager/Contacts gives a missing controller error, instead of my plugin controller index:
You should be able to access your plugin's Contacts Controller (without setting up routes) from /contact_manager/contacts. So by the looks of your question the absolute URL should be:-
http://localhost/cakephp/contact_manager/contacts
If this is not working then there's something wrong with the plugin's setup. Make sure the plugin files are readable and that the filenames and classes are all correct. Your plugin structure looks good otherwise.
If the above URL works then (and only then) you can consider re-routing it. To do this you need to do something like this (preferably in /app/Config/routes.php):-
Router::connect(
'/ct/',
array(
'plugin' => 'contact_manager',
'controller' => 'contacts',
'action' =>'index'
)
);
If the route does not work try checking that nothing is overridding it elsewhere in your routes file.
You should use snake case (e.g. 'contact_manager') not camel case (e.g. 'ContactManager') in your route parameters.
This is my answer ---> thanks all
http://i.imgur.com/Ju8mq4D.jpg
in Controller:
AppController.php
ContactsController.php
in Model
AppModel.php
Contact.php
in View/Contacts
index.ctp
app/Config/routes.php
Router::connect('/ct', array('plugin'=>'ContactManager', 'controller' => 'contacts', 'action' => 'index'));
app/Config/bootstrap.php
CakePlugin::load(array('ContactManager' => array('bootstrap' => true, 'routes' => true) ));

CakePHP Plugin - Error "Controller could not be found"

I'm currently using CakePHP 2.5.2
I want to integrate a plugin to manage Users.
I tested it in a first time as a single CakePHP Application (controllers in app/controller, models in app/model, views in app/view/Users): was ok.
I'm trying now to convert it as a plugin:
I've created a folder UserManager in app/plugin.
When I try to go to the url of one of my controllers, I get the message Missing Controller.
All plugin are loaded in Bootstrap.php (CakePlugin::loadAll();).
I tried to find similar problems vs solutions but no one was relevant with my problem (I tried some proposed solutions but root causes were different.
When I look at DebugKit in Include section, I can observe that my DebugKit plugin is loaded but not my other plugin...
Could some one suggest me a solution ?
Thanks in advance.
(Please find bellow a description of the code)
I added controllers, models and views as follows (skeleton generated by Bake and checked: ok):
1) Models in app/plugin/model
UserManagerAppModel.php
<?php
App::uses('AppModel', 'Model');
class UserManagerAppModel extends AppModel {
}
?>
User.php
<?php
App::uses('AuthComponent', 'Controller/Component');
class User extends UserManagerAppModel {
...
}
?>
2) Controllers in app/plugin/controller
UserManagerAppModel.php:
<?php
App::uses('AppController', 'Controller');
class UserManagerAppController extends AppController {
}
?>
UserController.php:
<?php
class UsersController extends UserManagerAppController {
public $uses = array('UserManager.User');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('*');
}
public function login() {
}
...
?>
3)Views in app/plugin/view/Users
Nothing special.
I think it is an issue with your path names: They have to include the plugin name. Plus the folder names have to start with a capital letter by convention. For example, UserManagerAppModel has to be in the following file: app/Plugin/UserManager/Model/UserManagerAppModel.php.
See also http://book.cakephp.org/2.0/en/plugins.html#creating-your-own-plugins
Since I believe has included AuthComponent settings in your AppController loginAction note the call and check the name of your plugin. In my case the plugin name is Barracuda, the error does not occur stopped until the call was made ​​for barracuda, with the first letter in lower case.
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => 'Barracuda'
),
for
'loginAction' => array(
'controller' => 'users',
'action' => 'login',
'plugin' => 'barracuda'
),

Adding a controller function for the "home" view in CakePHP

When visiting a default CakePHP site, it takes you to "home.ctp" page.
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
I want to add a few elements there (like blog posts), so I thought I could just add this to the PagesController() class:
public function home() {
$this->set('blogposts', $this->Blogpost->find('all'));
}
But that does not work.
So: what is the correct way to add something like this on the home page (or any other page for that matter)
The preferred option is to create a custom route for the home page but you can also override the PagesController's display function
Option 1: (Preferred Method)
Router::connect('/', array('controller' => 'mycontroller', 'action' => 'myaction'));
Option 2
Router::connect('/', array('controller' => 'pages', 'action' => 'home'));
Option 3:
class PagesController {
function display()
{
// default controller code here
// your custom code here
}
}
The final option is using requestAction in your view, but it isn't recommended as it has a huge performance drawback
Option 4: (Not recommended)
$newsitems = $this->requestAction(array('controller' => 'newsitems', 'action' => 'getlatestnews', 'limit' => 10));
In fact, the action is display, home is a parameter. So your main method in the Controller Pages must call display, not home. After that, create display.ctp view.
Reference:
Routing
To actually answer the original question:
$this->loadModel('Blogpost');
Ideally the model should be called
$this->loadModel('BlogPost');

CakePHP Custom Route Classes - How to Pass Arguments?

I've created a custom route class and I want to be able to pass in settings/options to the constructor so that it's configurable. Can this be done?
Documentation for Custom Route Classes:
http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes
My custom route class:
https://github.com/Signified/CakePHP-Model-Route-Class
You can probably just pass any settings/options you might have in the options of your Router::connect function.
App::import('Lib', 'ModelRoute');
Router::connect('/', array('controller' => 'pages', 'action' => 'display'),
Array('routeClass' => 'ModelRoute',
'someMoreOptions' => 'OptionValue' ));
Then you can retrieve the key someMoreOptions in your constructor
public function __construct($settings = array())
{
$this->settings = Set::merge($this->settings, $settings);
// Now you can do something with the option passed.
if(isset($this->settings['someMoreOptions'])
DoSomethingWith($this->settings['someMoreOptions']);
}

Resources