How to change only frontend language in Cakephp 3 - cakephp

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.

Related

Creating static home page for CakePHP3

I am working on a CakePHP3 project. I want a static homepage that will be loaded on www.mysite.com.
For this I have created a PagesController which will handle all the static pages in the website like about,contact,etc.
I am having display.ctp view in Template/Pages/display.ctp to load on www.mysite.com.
But, for testing (routes are not configured yet), I'm using www.mysite.com/pages and www.mysite.com/pages/display to show the view but it gives error as
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mysite.pages' doesn't exist
Do I need to create Table for this ?
It is much, much easier than that
For this I have created a PagesController
There is already a pages controller for serving static content, and a
static template for the home page, there is no need to create/overwrite the default pages controller, to replace it with the same (or less) functionality. This is also mentioned in the documentation
The steps to modify static pages (with default routes) are:
edit src/Template/Pages/home.ctp - look at the url /
create/edit src/Template/Pages/something.ctp - look at the url /pages/something
The error means that the application is looking for a model named Page. To tell the application that your controller does not refer to any model you have to use something like bellow. Also add the proper action. www.mysite.com/pages/display means in controller "pages" call action "display".
class MyController extends AppController {
var $uses = false;
public function display {}
}

CakePHP : having different layout files for backend and frontend

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

How to preserve layout through different views of same controller in cakephp

I'm using cakephp and I have set a simple site, when rendering index() it works fine
but when programming other methods of the same controller, the views for them do not show the
background, it's like it cannot find the images, I thought the layout would be preserved for all views.
Your View/Layouts/default.ctp layout file will always be used unless specified otherwise. If it's not showing a background, it's like you're using an incorrect path for the image, css...etc.
If you want to apply a layout to all methods of a particular controller (but not all other controllers) then use a theme.
Controller Code:
class MyThingController extends AppController {
public $theme = 'MyTheme';
....
}
Next you have to put your layout file in:
/app/View/Themed/MyTheme/Layouts/default.ctp
Then all methods in your controller will use this layout by default.
See here for more info: enter link description here
(note: this answer applies to Cake version 2.1+)

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.

Cakephp: Routing and default views

I'm currently trying to add an admin mode to my cakephp site.
I followed a tutorial on the web and added a routing prefix:
Configure::write('Routing.prefixes', array('admin'));
Then I implemented login and logout functionality.
I added a admin_view.ctp and admin_index.ctp to a model where I want to restrict access. Therefore I deleted view.ctp and index.ctp and expected that only admins could view the model by using:
http://xxxx/model/admin/index
But when I entered
http://xxxx/model/index
a default view appeared that I could not disable (it allows model manipulation). Is there a standard way to disable all these default views or do I have to create a index.ctp and view.ctp that simply show error messages?
If you want to disallow certain action, you need to setup ACL, which is described here. And for authentication, in your controller you need something like this:
class SomethingsController extends AppController
{
var $components = array('Auth');
//this will allow all users access to the index
//and view methods ( but not any other)
function beforeFilter() {
$this->Auth->allow('index','view');
}
// other actions
}
Hope this helps.
Delete default controller methods: function index(), view(), add(), edit() and delete(). Removing *.ctp templates is not enough.

Resources