CakePHP: Embed CTP filename and path in views and elements - php

I'm looking for an elegant way to make a bit of jquery aware of the filename and path of the ctp file containing a given dom selection. I'm using simple_html_dom to write changes to template flies via an ajax call. I need to provide the filename and path of the view or element template ctp file that contains the dom element I've selected in order to write changes to the correct file.
Outside of manually passing this information into each and every view and element in my application, is there an elegant (and secure) way to make this information available to jquery?
Additionally, is there any reason I should be worried about embedding this information in the document in plain text? What is the most appropriate location to store it? My first thought was in an element's title attribute, but is there a more appropriate location?
Thanks!
UPDATE This question may be better posed as: what is the best way to transparently extend or modify every view and element in cake? I'm passing some information down from app_controler in beforeRender now, but that doesn't allow me to pass information about individual element view files as far as I'm aware. Ideally, every view and element would look for the presence of a certain classname in dom elements and insert path and filename information in the title attribute for that element before rendering. I can't figure out a good way to do this!

<div rel="<?php echo __FILE__; ?>">...</div>
unless im not understanding you

You could create your own View class that handled this for you. Inside your 'views' folder, create a file named (for example): custom.php.
This file should contain the class that extends the base View class of CakePHP, named 'CustomView'. Then, in your AppController's beforeFilter, set $this->view = 'Custom'. I'm not sure which method would be best suited to what you want to do, but render() or renderLayout() are probably a good place to start looking.

Related

Passing many argument to controller function in Codeigniter

I have a controller named as Tag which is related to handling posts related to certain tags as wish by viewer
I want to make URL look like this
http://www.mydomain/tag/xxx/xxx
where the viewer can filter the post via using as many tags as they like
e.g
http://www.mydomain/tag/XXX/XXX/.../..../...
I have tried this via using regular expression in routes.php
$route['tag\/[a-zA-Z0-9\/]']='tag/index';
and filtering the URL in index method of Tag controller but it didn't work for more than 1 tag. I want the user can pass as many tags as they wish. The methods said in this question also didn't work.Is there any way to do this for index method?
If you are doing this way, Hacker might be hack your application and harm valuable data.
It will make your site vulnerable.
so,I think you should go for another way...
/controller/method/red+tall+fat

Passing in page specific content into a PHP included header

I am creating a website for a local company and I am splitting off the header for each page into a individual php page which I then include into each page. I was wondering what would be the best practice to insert individual description and title content into the php header for each individual page. Should I create a php variable before the included header.php link and then insert that variable argument into the title and description tags in the php file? Each variable would have different titles and description depending on what page it's on. Or would it make more sense to someone include these different description and title content into another external php page? If you have a even more correct/easier way that would also work best with SEO please let me know. Thanks!
I would suggest creating a Metadata object definition, with all of the properties required (description, tags, etc). Then instantiate a Metadata object for each page of the site, just before including your header, and use the values of the current Metadata object in the header.
split your logic (and titles, page information, dynamic content) from your templates via an MVC type architecture
here are some easy to learn templating engines (and I believe the two most popular)
http://www.smarty.net
http://twig.sensiolabs.org
You can read about MVC architecture here: http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller
However you do it, basically what you suggest initially is one of the best ways. Define a variable, or object, or whatever, in each page before the inclusion of the header.php file (again, whether that's a template, or just a regular php inclusion, up to you). Then just set them to whatever you want for each page, and you're good to go.
Personally, I would recommend using a template engine like Smarty.
As for SEO, that's an entirely different conversation. That's more related to the content of the variables, as opposed to the implementation of them.

Reading PHP file with fread

I am working on cake php.
I want to develop a generalized application in which i will give in folder names.
Those folders will be basically the links of other application, also on cake php.
Now the main objective is that my app will go inside the controllers' folder and first list out all the controllers, and then look inside that controller and read each inside function, and show them as separate functions.
I used fread and file_get_content,
but they are not reading properly even the string which is returned as file contents has some text missing from start.
Adding to that i remove the starting php tags from controllers it reads whole file but the thing is in this way the controller wont work.
anyone having solution of this please ?
You could use Folder class from CakePHP core or even DirectoryIterator to get a list with all of your controllers. Then instead read the content of your files you could use ReflectionClass to collect information about your classes. ReflectionClass::getMethods() can help you to build your methods list. You could also consider just the public methods if desired making use of ReflectionMethod::isPublic() method.
Depending on your implementation you'll have to load the controller classes

ZendFramework Headscript Helper - Make scripts unique

How can I ensure that the headScript and headStyle helper include css files only once when added?
The reason I am asking is that I would like to display some contents in a lightbox and all Flashmassages.E.g. notifications like profile successfully edited.
To display flashmessages in the lightbox i would like to check at the top of my layout script if they are set, in case they are i would like to attach the required javascript library using the headScript helper.
The problem is that I have no control about the scripts which were already added at this point. Maybe at a page where the library is needed for an other use case, it has already been added with addScript.
How can I ensure all scripts are added only once to my helper?
I already checked that these helpers extend Zend_View_Helper_Placeholder_Container_Standalone which uses an ArrayObject internally to hold the data and provides getters & setter to the array object.
Maybe a solution here would be to check each time when adding a script file if it already exists using the ArrayObject?
As long as the paths and filenames are the same, the files will only be added once. The Zend View Helpers should take care of that for you.

CakePHP - include css based on specific pages?

I want to include a specific css file that should be applied to the homepage, and 6-7 other pages throughout my site.
I know I can do this via PHP, getting the URL, finding out what page, linking the css...etc, but I was wondering if there was a slick way (or any better way) using CakePHP to include the correct css file(s).
I realize I can link the CSS file from the specific views, but - then they wouldn't be in the <head>. Is there a way to link from a view and have it show up in the head?
I hope my questions make sense, and greatly appreciate any help.
I realize I can link the CSS file from
the specific views, but - then they
wouldn't be in the <head>. Is there a
way to link from a view and have it
show up in the head?
Yes, they would be in the head. See the HTML Helper documentation:
If key 'inline' is set to false in
$options parameter, the link tags are
added to the $scripts_for_layout
variable which you can print inside
the head tag of the document.
So, this snippet in your view...
$this->Html->css('my-css', null, array('inline' => false));
...will add the proper <link> element to your <head>.
Check this little tutorial out:
http://nuts-and-bolts-of-cakephp.com/2008/05/05/css-files-and-scripts_for_layout/
Basically you can use this standard view attribute $scripts_for_layout to inject CSS files based on the view. Hope this is what you're looking for. There's a few other crazy options I thought of, involving extension parsing, but it would probably be more cumbersome than just manually linking the stylesheets. I think this link describes the best solution.
You can also have different layouts, and include css in them:
http://book.cakephp.org/view/1080/Layouts#!/view/1080/Layouts
This comes handy if all the views of a model have the same css, or script.
It is not the answer for your question, but...
you can program your own injection for the layout file. The problem with $scripts_for_layout is that js and css codes are injected in the header. If you write your own implementation you can put the $scripts_for_layout var (for the js) at the end of the layout file. The cue is: separation...

Categories