CakePHP : having different layout files for backend and frontend - cakephp

Is it possible to have 2 different layout files in the app > view > Layouts folder. The reason that I'm asking this is that I already have my backoffice, but don't know how to proceed in making the structure for the frontoffice.
The routes of the views in my backoffice use the prefix admin and use the default.ctp as layout. I actually want that file to be admin_default.ctp so I can use the default.ctp for my frontoffice views.
My question now is how can I separate those layout files. What I want to achieve is that some controllers/views use default.ctp (front) and other controllers/views use the admin_default.ctp (back). Is this a good approach or is it better to consider an alternative?

simply you can use $this->layout to specify your custom layout in controller.
this article may be useful for your question

In your AppController's beforeFilter, just check if it's admin prefix, and set the layout accordingly:
class AppController extends Controller {
public function beforeFilter() {
if($this->params['prefix'] == 'admin' && $this->name !== 'CakeError') {
$this->layout = "admin"; // set the layout
}
}
//...
Another nice thing you can do after determining it's the admin is is force SSL (often a good idea for back-ends):
$this->Security->requireSecure(); // inside the above if block

Related

How to change only frontend language in Cakephp 3

I have created multilingual website in English and Danish language using I18N and .po file. But if I change the language using I18n::locale() it changes the language of admin panel too. I want to change the language of frontend only. Please suggest.
Thanks in advance.
Changing the locale the way you're doing it is correct:
I18n::locale('da_DK');
But if it's changing it in your admin panel too, then the question is purely about how your application is structured and where you are changing the locale.
If you set the locale in AppController for example, then it's going to change for all controllers. There are many ways you could do it. You could create an AdminController which handles an /admin prefix, and changes the locale just for that prefix.
namespace App\Controller\Admin;
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\I18n\I18n;
/**
* Application Controller for `admin` prefixed controllers.
*
* All controllers within the `app/Controller/Admin` directory should extend this
* controller rather than the normal `AppController`.
*/
class AdminController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
I18n::locale('en_GB');
}
}
Then ensure you have correctly set up a prefix route for /admin so that you can put all your admin controllers in app/Controller/Admin, and all those controllers should extend AdminController instead of AppController.
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
I didn't want to change the flow of my website. So I used 18n::locale('en_GB') in my all front-end controllers. That resolved my problem.

How to have multiple layouts for cakephp?

I am quite confused on how to have my own layout for each page in cakephp. Currently, there is a default.ctp which I have modified to have my main layout and included the $content_for_layout code. So whatever I have entered in the pages\home.ctp gets reflected. But I want to have a login and register layout and also their individual pages. How can I go about achieving this? Should I even edit the default.ctp? Or create another layout for my main page?
Please assist.
You can specify a different layout in your Controller methods, e.g.
function index() {
$this->layout='my_index_layout'; //app/views/layouts/my_index_layout.ctp
}
function view($id) {
$this->layout = 'my_view_layout'; //app/views/layouts/my_view/layout.cpt
}
But I want to have a login and register layout and also their individual pages.
The "layout", as understood in Cake, is mostly the header and footer. And it sounds like you are referring to the layout of content. You can do the layout of content in each individual view file.
Should I even edit the default.ctp? Or create another layout for my main page?
Yes, it is there for you to modify. If you want more layouts, you can create more in that folder and specify the layout in the controller (otherwise, it defaults to "default" layout).

CakePHP global variables in model

I am making one CakePHP project with Auth component. When I log in I got Session variable with user data. At the moment I am using this variable in controllers to pass data to the model.
$user = $this->Session->read('Auth');
$costs = $this->Posts->get_quartal_cost($user, $quartal, TRUE);
As I am using this in many controllers/models I am thinking that this is not DRY approach, so I wanted to make it better - something in AppModel(?)
Do you have some advice how to do that better?
Thanks
You could use the beforeFilter event in your AppController and do something like this:
public function beforeFilter()
{
if ( $this->Session->check('Auth') )
Configure::write('Auth', $this->Session->read('Auth'));
}
From anywhere in your controllers, models and even views, you'll be able to access it by using echo Configure::read('Auth');. See the Configuration class documentation for more information.

Customizing layouts with CakePHP based on the current record

Does anyone have recommendations on how to customize the layout sitewide based not on the current view, but the data associated with the view? In most cases, the models we are using have an associated Club id, so would have to customize the layout header image, css, etc, depending on which Club the current page is associated with.
Here is what I am thinking so far.
Call a function in the model associated with the current controller, which gives you the layout parameters.
In appController, beforeRender, set layout parameters for the club.
This doesn't seem very elegant, because each model would have to have this function, and how would I call the right model if beforeRender is defined in the app controller?
Some tips would be great!
Russel,
you might want to look at this.
Simply create a file in layouts called "dashboard.ctp" then if your view is index, your function would be something like this...
function index() {
$this->User->recursive = 2;
$this->layout = 'dashboard';
$this->set('users', $this->paginate());
}
The important line being $this->layout = 'dashboard';
What we ended up doing is checking in the app controller for the domain the site is currently being loaded from, then setting the layout based on this information (have a layout for each domain the site needs to look customized for). Not a great solution but it works fine.

How to load custom plugins in CakePHP?

I'm writing a poll plugin for a website based on CakePHP. The plugin works good if I access it from its own URL (eg. myapp.com/plugin/controller) but I need to call it from different pages. I would like to include it as a widget in every page.
I'm looking for a method like $myplugin->renderPoll($pollId); but I really didn't find any information about how to instantiate the Polls class. I tried with App::import and ClassRegistry::init with no luck.
Any suggestion ?
Thank you
Looks like you are trying to create some sort of Helper to create poll cross views? I would suggest creating a Helper for that particular class. Simply create a helper in plugins/plugin_name/views/helpers/foo.php, and in each controller (or in app_controller.php) that you need it, include the helpers as $helpers = array("PluginName.Foo"); and inside your view, you should be able to use the methods defined in foo.php by calling $foo->renderPoll($pollId).
//app/plugins/plugin_name/views/helpers/foo.php
class FooHelper extends AppHelper {
var $name = "Foo";
function renderPoll($id=0) {
//...
}
}
Use Elements! They're small blocks of presentation code that need to be repeated from page to page, sometimes in different places in the layout.
Check this link out: http://book.cakephp.org/view/1081/Elements
I guess this link explains everything you need.

Resources