Reading PHP file with fread - php

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

Related

Can I include php file in .tpl?

Can I include a php file to tpl?
All I need is data from the database. I tried with required('path/file.php') but without success.
I already have controller for that. I need to include controller/file, class/php extension anything just need in tpl. I use OpenCart version 2.3.
if you need data from the database, in MVC a model should be used, and then let the controller add it's data to the $data array, which the is available to the template.
this shows how OpenCart actually works (source):
now tell me how your attempt to change a template (view) fits into there (notice the vertical lines).
just load the model into the controller and add results to $data, then it's available in the template.
You should not directly mix templates with custom PHP code, even if in some template engines it's possible.
You should rather look into means of providing your PHP logic through functions, filters or macros. This way you make your PHP code available in templates.
If OpenCart uses twig, you could look into how to do such in the documentation about extending twig here.

Pass Variables to Included PHP file inside a Joomla Article

I'm new to Joomla, but not a new programmer. I've written several applications in PHP that I want to include inside Joomla articles. Simple enough:
<?php include 'file.php'; ?>
The issue is that inside the PHP files I have a bunch of code gathering and creating variables that I need to POST and retrieve. I can get those POST variables inside the Article, but I can't pass them back to the included PHP file.
I've even coded the included PHP files to access the Joomla framework hoping to retrieve Joomla user id for example. This won't run inside the Article either and returns empty. However, if I run the PHP file on its own outside of the Article, I can access all POST data (obviously) and also the Joomla JFactory data. So it runs fine, until it's placed as an included file inside an Article.
The only way I've been able to pass something to the included PHP file is using $_GET url variables like this:
<?php include 'file.php?data=something'; ?>
However, this simply isn't practical as I have too many variables to pass like this. Normally, included PHP files run as part of the parent script and have access to all variables. How can I accomplish this in Joomla??
Much appreciated!
It is not recommended to include php into articles. Try instead one of these approaches:
Load your code in the index.php - file in your template: /templates/yourtemplate/index.php. This file is called every time your page is called.
Make a template override of the component where you want you external php file to be loaded. If this is in an article, you copy /components/com_content/views/article/tmpl/default.php (and possibly also other files there) to /templates/yourtemplate/html/com_content/article. Then add your include-statement here. (info) This file will be loaded each time you view a single article, but you can have further logic to only run it if (whatever)...
Use Jobin Jose's approach if you want to load you php-file inside content in an article. (info)
Some other approach writing a plugin
...or a component
I would say probably the easiest method is 2. (or 1.), but it all depends what you want to do.
Try this,
You have to create a simple module for your requirement, means what you are trying to achieve with included php file.
then place that module within the article with {loadposition module_position}
then you will get all the POST variables to that article and also suppose user_id and all other joomla Factory can be accessed bcoz its a Joomla module.
for a quick tutorial about module creation can be found here.
hope it make sense.
I resolved this using an Extension called Sourcerer
https://www.nonumber.nl/extensions/sourcerer
Variables can be passed without issue now.
Thanks for the input.
Why do not you try to create a new Joomla component and do whatever you want it it?
I am absolutely beginner in php/mysql but managed to do it with my page, following this instructions:
http://www.inmotionhosting.com/support/edu/joomla-3/create-component/helloworld
When you create this component, whatever you write in its "default.php" file, is actually a blank HTML/PHP page, with your joomla design and some other Joomla-features.
Sorry for the amateur answer and terminology :)

PHP layout (template) page

I am new to PHP, lately I was working mostly with .NET MVC 4.
In MVC you can make a layout very easily, end every view knows to use it without telling each view seperatly.
I am trying to figure out what is the best equivalent in PHP.
I have not found a satisfactory answer yet. The best on was to create a 'header.php',
and for each view add a require/include.
I think this breaks DRY, because on each view i need to specify the header. What if I decide to change the file??
Does anyone has a better suggestion?
I am using codeigniter also.
Thank you!!
As you are using CodeIgniter, there is absolutely no single reason to touch require/include commands.
You can load your header views either from the controller or from the main view. If you are worried about file name changes, create a new config file, add it to autoload.php and import your filenames via $this->config->item('config_var_name')

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.

CMS permalink and templating system how to?

I want to understand how to use templating system and permalinks on php websites :D !..
let me describe my self more,
1.currently i have 20 files each have its own php logic (index.php,wizard.php,search.php etc)
ALL use same class's and includes.(install.php include all the required for all class's in my project abd u require_once(install.php) in all files)
i wana remodel my website into.
Analyze URL requested ---> IDENTIFY requested page ---> GET TEMPLATE for THIS PAGE -->MODIFY header(meta) and footer(javascript) ---> add logic ---> display page :D.
can some one put me on right learning track :D !. cuz i hv coded my website fully in oop and made all its content loads dynamically from MYSQL (simple small CMS) but i have no clue how to join template and php into index without repeating my self and create different file for each page in my web ! (each file do different jobs of course like file users.php does login and registration and userprofile etc)
Hope my english wassnt too too bad and u could understand my question :D !
My current approach so far:
Mysql table : page_tbl
columns: pagename,LogicFile,templateFile,MetaTag.
index.php?pagename
will check if not already cached or not listed for chacing it will :
Mysql:SELECT * WHERE pagename='$_GET['page']'
loginfiles = cars.php,search.php (will be exploded with , and included)
Template files = will be also exploded and modified according to MetaTag.
Metatag: Serialized assoc array with ['name']=['value'];
Then i start buffer output , replace template with new descriptions and keywords(auto generated)
include logic files
include footer.php (which include the scripts)
am i near to correct rout or still far away ? or am did i lose my track and over killing
If you are unsure whether you want to write your own, or use an existing one.
It is going to be a quite possibly very rewarding experience but a very time consuming one to write your own.
If you have a task at hand that you need to be solved, use an existing one.
That being said there are plenty of templating systems, smarty being the most long-lived one. You can find a short discussion on 5 popular ones at phpbuilder.
You also have entire Frameworks that you might want to consider. They are more than just a templating system, where database stuff, ajax helpers etc are included. Zend, CakePHP, and Codeigniter being popular ones. You can find comparisons of these at phpframeworks.
The best way is to take this route:
parse request data
determine controller which will be used
in controller select layout and template [layout "includes" template, it hold the contents that are common to all pages and template has the request-specific content]
in controller also fetch the data from database [MVP way]
assign data to array and pass it to the layout, it'll pass the data futher to the template
in layout and template use data from that array and construct view
pass everything to the browser
This is the way the frameworks work, and it isn't that bad. ;]

Categories