i've just started using cake 2.2 (usually i use 1.3) and i have a problem with view elements. i need to create an element for my application, being used in homepage and some other places.
i think i've done all the things right:
i have an action in the controller (ads/latest)
i've created the element in app/View/Elements/latest.ctp
i call it in the home.ctp like this: <?php echo $this->element('latest'); ?>
but it gives me this error "Element Not Found: Elements/latest.ctp"
A few things could cause that:
there's a difference between the filename and the element() call
the element call should NOT include ".ctp"
the element file has an incorrect extension (should be .ctp)
file permissions on the element are incorrect and don't allow access
the path to your element is incorrect (ie it's in a folder, but you're not specifying that in the call)
If you pass all those tests, you should be good to go and not get any errors.
Related
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
I find a problem when i develop application via cakephp.
for example: my url is http://localhost/controller/view/id this is working fine.
BUT, when i append more invalid parameter, it still works,
like http://localhost/controller/view/id/adfasd/adfasdf/asdfasdf/asdfasdf
It should show up 404 page not found.
Shall i need to use $this->passedArgs to check pass parameter manually in controller then throw exception? Or is there any configuration?
How can i deal with this case
Thank you
You should first look here Cakephp, Routing-Named params to find out how to properly use them.
As you should add which one to use, you should also add a regex to your id in the route.
Also when sending the data to an action you should throw the exception there like it is explained here: cakephp deal with passing wrong parameter in url
I am using cake 1.3 and i had tried to implement favicon..But some strang error happens and sometime its showing favicon but for sometimes its not showing it.Pronlem is cakephp img folder path is changing>How can i escape from this problem.
i have used below code in my default.ctp
<?php echo $this->Html->meta('favicon.ico','../../app/webroot/img/favicon.ico',array('type' => 'icon'));?>
Favicon shows for following url.
http://localhost/finalportal/index.php/events/eventlist
Favicon not showing for followin url
http://localhost/finalportal/index.php/productsServices
I had tried this also.
<?php echo $this->Html->meta('favicon.ico',/favicon.ico',array('type' => 'icon'));?>
in this case favicon path is not correct
What i am doing wrong
meta('favicon.ico',$this->webroot.'img/favicon.ico',array('type' => 'icon'));
It works perfectly, just create favicon.ico image in img folder.thats it.
Don't use relative paths in the HtmlHelper, Cake prepends the correct path for you.
I haven't use the meta function, so I'm not sure if it respects the Cake directory conventions (e.g. images are in img, JavaScripts in js), but this should work:
<?php echo $this->Html->meta('favicon.ico','/img/favicon.ico',array('type' => 'icon'));?>
use following code to get right path.
<?php echo $this->Html->meta('favicon.ico',$this->webroot.'img/favicon.ico',array('type' => 'icon')); ?>
The problem in the first version is that you're using relative paths so it will always end up pointing to the wrong place, depending on how many parameters the URL has. ../../app/webroot means "two levels down, then to directory app/webroot". Two levels down from http://localhost/finalportal/index.php/events/eventlist is http://localhost/finalportal/index.php/, but two levels down from http://localhost/finalportal/index.php/productsServices is http://localhost/finalportal/, so you end up in the wrong place.
The reason why the second method (which is syntactically correct) doesn't work is probably because you have the server set up wrong. Apache's DocumentRoot should point at the app/webroot directory or the .htaccess file in the root directory should redirect requests to app/webroot.
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.
I have a fresh CakePHP 1.3 install and it currently has one layout. I am about to add a few more but I don't want to have to keep coping and pasting the Header and footer in to each layout.
At first I thought that I could do this with an Elements, but it does not seem to render the Configure::read('var_name'); chunks while in an element.
My other thought was to create a common layout and use lots of variables to add and remove sections from the screen depending on what type of user they are... but this would be troublesome to say the lest.
My question is:
Is a way to include a header/footer section in to a layout while getting the Configure::read() function to output text?
I still think that elements are the right way to go for this (shared view snippets, FTW). I have to admit that I'm a little surprised that elements can't read from the Configure class, but I'll concede that I haven't tried it. If that really won't work, then try passing the values directly to the element:
<?php echo $this->element( 'partial_name', array( 'var_name', Configure::read( 'var_name' ); ?>
In the element, you should then be able to access the variable simply as $var_name. For more on passing variables to elements, take a look at the [Passing Variables into an Element](Passing Variables into an Element) section of the element documentation.
Hope that helps.
Create element with new header suppose new_header.ctp. Then put element('new_header')?> in your preferred position layout