Should I create a new controller in cakephp - cakephp

I'm new in CakePhp but experienced in CodeIgniter. I created a controller in "WelcomeController.php" in controller directory and run the page. I got two errors
1. Error: The view for WelcomeController::index() was not found.
2. Error: Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
My question
Why I am getting this error even though I have supplied index() function?
In codeigniter we may not create a directory for a view. I don't want to create a directory "Welcome" in view. I there any provision provided?

In Cakephp you have to create view for function or here it called action. In your case, Create index.ctp on App->View->Welcome folder. This Getting Start
will give you a basic idea.

1) You're getting that error because your missing the view, not the controller function. To fix, do what the error suggested:
Confirm you have created the file: C:\xampp\htdocs\myc\app\View\Welcome\index.ctp.
2) "I don't want to create a directory "Welcome" in view. I there any provision provided?".
Not really... I mean, no if you want that action to have a correspondent view to put the content. Otherwise you can use $this->autoRender = false to not show anything... But that'll mean the url localhost/welcomes/index will be blank.
I recommend you read the basics as Fazal said. I know every framework can give us "quirks" and we end up expecting every other framework to work the same way we are used to, but try to adapt to the cake-way.
Btw, should be "WelcomesController", according to cake conventions

Be sure that you have all the files and directories necessary for the modal, controller and view to link up correctly i.e.: Create the folder called welcome(s) in your views directory with an index.ctp file. This should get rid of that error.
Check out this brilliant blog tutorial: Link

Related

Writing custom php script in joomla

I am good in php and javascript. I need a guidance where to place my custom php script if i make a ajax call from joomla.
I need to fetch the file names from a specific folder. I could make an ajax that will communicate with my php code and in return, my php code will give me the files of the specific folder.
Where is that php script to be place and how it should be routed.
For example
$.ajax({
url:'....../fetchfiles.php',
success:function(data){
}
})
Hope i was clear.
If I understood correctly your question my answer would be that you can place your fetchfiles.php wherever you want providing that you put right path to the file in your $.ajax call.
Then in the fetches.php you should call Joomla framework before you start your code, something like this:
// no direct access
define( '_JEXEC', 1 );
// get joomla root
$jpath_root = $_POST['jpath_root'];
//load Joomla
define( 'JPATH_BASE', $jpath_root );
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
jimport( 'joomla.application.application' );
jimport( 'joomla.filter.filteroutput' );
$japp = JFactory::getApplication('site');
/* now you are good to go with your code */
Just a few words about
// get joomla root
$jpath_root = $_POST['jpath_root'];
part.
My experience is that it is the best if you pass JPATH_ROOT variable via $.ajax( one way would be to have hidden input with value set to 'JPATH_ROOT' in your joomla file from where you call ajax and then in your ajax.js just pick that value up and pass to the ajax.php or fetches.php in your case)
You can define JPATH_BASE another way too, eg
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
or
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
depending on your actual files structure but be aware that this way you practically redefined original JPATH_BASE constant and that it may cause conflicts in case you call, later within your code, some third party component functions/classes which may depend on original Joomla defined constants ...
I've learned it hard way with ZOO Component and my ajax calls ... :)
Hope this makes some sense.
Based on where you want to show the results you can create a component or a module (you can also easily include the module in an article to print the result in component position).
In the case of module:
https://docs.joomla.org/J3.x:Creating_a_simple_module/Developing_a_Basic_Module
Obviously your ajax call will be in tmpl/default.php and your php script in the module folder, simply you have to include it in the file list in mod_helloworld.xml. Then zip your folder and install it using Module Manager.
In case of problem with your jquery code, you have to use the noConflict() mode.

getting the field values of content types from drupal

Here I want to know how to get the field values of my custom content type 'mypop'. I tried all methods in google but I don't know how to use, for example i tried function node_load, I can't able to know where to write this function, what are the parameters and tried EntityFieldQuery too. Can I know the how to do it in brief explaination.
Thanks in Advance.
Definitely a very broad question. Assuming you have the content type 'mypop' created already, think the easiest steps for you would be to:
Make sure you create some content of that content type
Customize the "Manage Display" on that content type and make sure the fields you want are set to visible there
Once you do this, those fields should be visible when you view the nodes of that content type already. If you want to further customize the view, you should probably customize the template file for that specific content type (there's other options but trying to keep this as simple as possible).
To do this, copy the "node.tpl.php" file you'll find on the modules/node folder to your theme templates folder and change it's name to be "node--mypop.tpl.php".
That way, you'll override Drupal's default display template for that specific content type only. Now you can basically tweak it into anyway you want.
Hope this helps!
Thanks a lot Alberto. Its working now! I have another issue too, it also got cleared and now its working fine !Another issue is javascript called automatically when I open views edit for other contents. Now by doing overriding this template file, it also gets cleared. Thank you !
node_load take node id. So in order to use noad_load() function, you should first retrieve node ids. Better if you use noad_load_multiple().
// Query all of the nids of a particular content type.
$nids = db_select('node', 'n')
->fields('n', array('nid'))
->condition('type', 'Article', '=')
->execute()
->fetchCol();
// Get all of the article nodes.
$nodes = node_load_multiple($nids);
You can see the result by calling print_r($nodes). Just write a normal function in you .module file or .inc file. Call it anywhere, your choice. say, in menu callback.

Controller Redirect issue in CakePHP

I have a controller with name users_controller, within the login action I want to redirect to my affiliate_redirect_controller.php, now I the following code in the users controller to redirect
$this->redirect(array(
'controller'=>'affiliate_redirect',
'action'=>'logRedirect' ));
And then I get the following error which I can't seem to resolve
Error: The requested address '/affiliate_redirect/logRedirect' was not found on this server.
I honestly do not know what this could be, quite new to cakePHP and none of the solutions found work for me.
the contents of affiliate_redirect_controller.php looks like this
class AffiliateRedirectController extends AppController
{
var $name = 'AffiliateRedirect';
function logRedirect(){
}
}
I can see there is a mistake in your code its because of naming convention.
$this->redirect(array(
'controller'=>'affiliate_redirects',
'action'=>'logRedirect' ));
Please see the above changes when you are writing your controller name in lowercase like above it should be plural affiliate_redirects and should not be affiliate_redirect
Apart from this you can use directly redirect as like this also.
$this->redirect('affiliate_redirects/logRedirect');
Please try, it should work.
Do you have a table in your database that corresponds to affiliate redirect controller?
You might want to rethink your logic, and use CakePHP routes to set the URL to what you want. Having a controller named affiliate_redirect_controller doesn't follow CakePHP's naming conventions.
Since I don't know exactly what you're trying to do, I don't know if this will work for you, but maybe consider redirecting to a separate action in UsersController like /users/affiliate_redirect/
Or you can create an AffiliatesController and then redirect to /affiliates/redirect/
Also, if you don't have debug mode set to 2, you should do that. It may help reveal what the actual issue is.
What debug level do you have in app/config/core.php ? Most of the time, when you get the message
Error: The requested address '/controller/action' was not found on this server.
it means you have a debug level set to 0 and increasing it to 1 or 2 allows to get more details about the error.

CakePHP Media Plugin Helper problem

I am trying to display an uploaded image with the Media Plugin of CakePHP.
I added the helper to the controller helper array: var $helpers = array('Media.Media');. Then, in my view, I have this code: echo $media−>file($news['Attachment'][0]['dirname'].DS.$news['Attachment'][0]['basename']);. But the problem is that, it outputs this error:
Undefined variable: media− [APP/views/news/view.ctp, line 3]
What could be the problem?
By the way, if a plugin has a model User in app/plugin/users/models/user.php and i create a new model called User in the app/models folder which one will be loaded?
Thanks in advance for any help!
First off if you are using 1.3.x refer to helpers via $this->HelperName->method(), there could be a variable called $media being set in some method. you can check this by doing var_dump($media);
The other option is that something has maybe unset it. Its very strange that you have the helper set but the variable is not set. It could also be due to adding the $helpers array to the wrong controller, you can try add it to app_controller and see if that works. if it does you had it in the wrong place.
If i got your second question correct, and we are talking about auto loading, a plugin controller will first look for the model in its own plugin directory, if it is not found there it will fall back to the app/models directory.
if you are loading it manually via the $uses array, it depends on the version of cake and how you do it. In previous versions 1.x even $uses = array('User'); would load the plugin model as cake would auto add the plugin prefix. This has changed for 2.0 afaik.
For other methods of loading a model, such as $this->loadModel('User); would load from app/models and $this->loadModel('PluginName.User') would load from the app/plugins/plugin_name/models dir.
Edit:
you are right that is funny having the error show $media- and there is the problem. did you copy that code from some site? − is not - you have a utf8 char in the code which is what its complaining about.

CakePHP Routing Alias, no prefix

I have a dashboard with a series of widgets. Per specification, the widgets need to be buried under a /widgets/ directory.
So I have added the following to my routes.php
Router::connect('/widget/:controller/:action/*', array());
But I seem to be running into trouble on widget/links/ and widget/links/view/1
I am new to CakePHP, but this doesn't seem all that impressive. I have yet to find anything in the Book or by search. So any help is appreciated.
Thanks.
Well...at the risk of stating the obvious...your route starts with /widget/, but you indicate that you're trying to access it via a plural URI (/widgets/). That's a problem. If that's just a typo, it would help to know what error you're seeing when you "run into trouble".
UPDATE:
Yes that was a typo. I corrected it. The error that appears for widget/links/ is: Error: WidgetController could not be found. It appears my index/default route is the main problem.
Given that information, it appears that CakePHP thinks that widget is your controller. Cake processes routes top down and finds the first one that matches. Ensure that you don't have a route above this one that looks something like /:controller/... or any other route above this one that starts with a variable.

Resources