cakePHP custom view folder - cakephp

I have a UsersController and index() action so by default cakePHP will look for the index.ctp in Users view folder. What i would like to do is to create a separate folder called Partials and have the controller look for the view inside the folder instead. Please help as I just started my journey in cake. Thanks

You can define the path to your view by $this->viewPath in your controller action. E.g. for the following folder strucuture:
... you can access the various templates by the following statements:
function index()
{
// by code conventions this action automatically matches /Home/index.ctp
$this->viewPath = "/Partials/"; // matches /Partials/index.ctp
$this->render("mypartialview"); // matches /Partials/mypartialview.ctp
}

Related

routing in CakePHP 3

I'm working on CakePHP 3.3
I have some dashboard controllers which are named like
DashboardUsersController.php,
DashboardBusinessesController.php,
DashboardCustomersController.php,
etc
I want to map urls like
http://example.com/dashboard/users/view/param1/param2
And this will call DashboardUsersController.php class and view function with param1 and param2 as parameters.
In short I want to change url http://example.com/dashboard-users/view/param to http://example.com/dashboard/users/view/param
this type of mapping will be done only if dashboard is present after domain, otherwise it will work as default like on accessing http://example.com/users/view/param1 will call UsersController.php
What I have done till now?
Since, I'm new to CakePHP and routing, I have no idea where to start from and therefore have done nothing till now. I need your help.
I think what you needed is prefix. Bake your controller model with prefix dashboard .
Use this in you routes.php
use Cake\Routing\Route\DashedRoute;
Router::prefix('dashboard', function ($routes) {
// All routes here will be prefixed with `/dashboard
$routes->fallbacks(DashedRoute::class);
});
And remove that dashboard part from controllers or remove dashboard from your table name and rebake everything with --prefix .
bin/cake bake all --prefix dashboard
These links will help you
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
https://book.cakephp.org/3.0/en/bake/usage.html

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 {}
}

how to call two different .ctp file in one function in cakephp?

I have 2 view files index.ctp and indexMobile.ctp.
i have to use one single controller logincontroller.php and a single function for both ctp file.
If you want to render a specific view use $this->render in your controller
$this->render('your_view_file');
see Rendering a specific view
Here is my example:
// Changing the "layout" (app/View/Layouts") if you want.
// Change this if you want a different layout for this view.
$this->layout='pdf';
// Folder where is the .ctp View file.
// This is also optional, if your view is in some other folder.
$this->viewPath = 'Pdf';
// Call the "{$view}.ctp" in "Pdf" folder (app/View/Pdf/{$view}.ctp).
// Use this for choosing the right View.
$this->render($view);

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.

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