I am currently developing a laravel app which I only managed the blog section with Wordpress. Everything works fine. Is it possible to call laravel function in my Wordpress function.php? If yes, how can I achieve that?
It will depend on what function you want to call. Some things can be used independent of the framework simply by including the packages, for example Laravel Collections. However if you need more in depth integration you should take a look at the following files.
public/index.php
bootstrap/app.php
bootstrap/autoload.php
It is in these files that the frameworks gets loaded. Meaning that it does all the prerequisites before your application code can run. If you wanted to use Laravel within WordPress you would need to mimic the logic from these files in WordPress. I have never done this before though, so I don't know if you will encounter any namespacing issues or anything else. You might also be concerned about the performance implications of loading WordPress and Laravel on every request. Good luck!
Related
I have an existing website running on PHP, MySQL CSS, JS and Ajax.
For future projects, I would like to use the Laravel framework to create new pages or functionality to the existing website... let's say a blog, for instance.
Is this possible?
Is the Laravel framework compatible with an existing website that is not set up using the MVC framework?
The reason is that I would like to make my work easier and more efficient and the use of a framework would be helpful.
Please let me know if you need any other info, I know the question is a little broad.
Thanks,
Justin
yes it's possible but of course maybe with difficulties.
you should put your web files in /views directory, convert your php files to controllers or library files and put your assets in /public directory.
then you can assign routes to your controller and call it form outside of the application.
main job is put your code into controller or libraries. is it possible to split your code into separate logical units? and extract model logic (as php mvc says) from your main code.
so if you have a clean code with good design and minimum dependencies you can think more about it. this could be a good challenge if you go through it cautiously. be careful to not fail and waste your time.
I am looking to implement Angular2 inside my current Laravel project. I've read about many setups including AngularClass's version with Webpack, but I feel like it's all over-complicated.
What I wish to make:
A simple single page app inside my Laravel app.
It needs to have something like Grunt/Gulp or even Webpack to compile upon changes.
Does not need NodeJs to run in production, no lite-server, just like AngularJS it can be injected and that's it.
I need to understand the implementation files to achieve this, so no starterpack or angular-cli, unless if you can supply good arguments.
Looking for a "how to"-like example on creating the above.
Thanks in advance.
EDIT: More information
As ANKH pointed out, i needed a more detailed and coursed question. So here we go:
- Looking for a example implementation of a Angular2 based SPA inside an existing PHP application (Laravel).
- I've tried many different tutorials, going from the heroes tour, to Sitepoint and AngularCLI based tutorials, but they all assume a SPA on it's own. Ergo, they are compiled and served through NodeJS, which I don't need.
Turns out that I've actually been looking at this entirely wrong. I've gone with using Angular-cli and found that I could generate the output files and include these inside my Laravel project. No need to integrate them further.
The Problem
I'm working on a site which utilises two PHP frameworks running in parallel, Wordpress and CakePHP. Both of them contain a core function called __().
I know I can modify the core files of either framework to check if the function is declared and then not declare it:
if (!function_exists('__')) {
function __() {
// ...
}
}
However, the problem is that both frameworks need to be automatically updated from time to time, and when this happens, the core files get rewritten and the function comes back, causing fatal errors.
Wordpress is out of our control. Editors using the CMS want to be able to update the framework and plugins automatically from the Wordpress admin panel. This means I cannot really modify the Wordpress core to solve the problem, as they will just overwrite my changes every time they update.
CakePHP is updated by the developers, via Composer. Of course one basic solution would be to ensure that all developers know that when performing a Composer update, the core file declaring the function needs to be modified to fix the fatal error. This is still a pretty bad solution.
The Question
So although I am 99% sure there is no permanent solution to this, I'm wondering if any PHP gurus can think of a really clever, albeit slightly unconventional/messy, way of solving this with some code in the custom application within CakePHP. If there is something that can be done which I don't know about in PHP.
How the Application Works
The basic marketing pages of the site are pure Wordpress. CakePHP does not load on those pages.
On some sections of the site which require more complex functionality, CakePHP is loaded first. Before the page is rendered, CakePHP loads the Wordpress framework, then renders the page through Wordpress. These are the pages where both frameworks collide on the same function being declared twice.
My Thinking
When CakePHP is bootstrapped on a page, let it run up until the view is due to be rendered. At this point, only CakePHP has declared the function.
Check if the function exists before Wordpress gets loaded.
Somehow prevent Wordpress from redeclaring the function, ahead of time. We write this in our application code, which is fully controlled by the developers and committed to our Git repo.
I feel ridiculous posting this question, as I'm sure this is impossible, but if there is a really clever way of solving this, it would be so useful.
As ceejayoz said, fork CakePHP, and maintain your project on that fork.
Make a commit that will serve your project and that you can submit to the Cake team. As you suggest, make __ declaration conditional on it not already existing, declare it #deprecated in its phpdoc, and make a namespaced version that you'll convert everything to. Send that as a PR to Cake.
When you need to update your fork, merge the upstream branch into yours. You much watch the incoming Cake changes for use of the __ function and change them accordingly. Git will not catch these.
I'm trying to integrate Wordpress in CakePHP 2.6.1 to use it as a CMS for my end users.
I'm including the required file to access wordpress functions: wp-blog-header.php.
As CakePHP only allows to include files located inside CakePHP I placed the wordpress intallation inside app/Vendor/wordpress and now I'm trying to import the necesary file in my AppController in this way:
App::import('Vendor', 'wordpress'.DS.'wp-blog-header');
But it seems there's some functions conflict as I'm getting this error from CakePHP:
Error: Cannot redeclare __() (previously declared in /home/todopeta/alvarotrigo/pruebas/cakephp/lib/Cake/basics.php:581)
File: /home/todopeta/alvarotrigo/pruebas/cakephp/app/Vendor/wordpress/wp-includes/l10n.php
Line: 163
Has anybody integrated wordpress in CakePHP before?
What's the way to go for it?
I've seen it seems to be possible with other PHP MVC frameworksk such as Laravel.
Is CakePHP more restrictive in this respect?
Access the WP data by creating a WP plugin for CakePHP and add a model for each of the WP tables and read the data via CakePHP.
Your linked article does the same but it uses the WP_Query class and further down Corcel which seems to be a mix between the Eloquent ORM and WP. I would not go that path and use the CakePHP ORM.
I can guarantee you that everything else is going to become a pain in the rear thanks to the fugly WP code and architecture.
If you try to use the WP code inside CakePHP like you've tried this will become super painful, time consuming and probably never work right without some really really fugly code.
If you need only parts from WP consider creating a WP plugin and embedd the WP content via an iframe in your page. AFAIK WP got a json API as well these days, you could try to read the data via that API as well.
You're going to have a nightmare and end up with a massive mess of code with this approach.
Instead, why not try and use a CakePHP CMS system, for example:
https://croogo.org/
First off, this isn't really a programming question but more of a programming concept question. Basically, I've built a bespoke PHP framework to speed up deployment on my end and I want some kind of plugin system in place that will allow me to add specific features to the base of the framework (like the SQL class or maybe a Twitter package) that will allow me to throw them into a folder and not have to actually edit the base for every new project.
Any ideas of the best way of going about this?
Here is a nicely written post by #ircmaxell on how to do that and what are the options:
Handling Plugins In PHP
Also check out:
Best way to allow plugins for a PHP application
what im doing in my cms:
for each plugin i make a folder latin-named of this plugin's name.
i create a /translations folder in there too. Check here.
have a single php file that has 2 basic functions, the plugin_install and plugin_uninstall (you know, things to happen on install/unistall like tables creation/drop)
create a special page of your system that reads these plugins, installed and not and give an on/off switch so users can install/unistall them.
load these single files mentioned above by a single call to include_once on top of your index page (or administration page) so to include whatever functionality they offer.
enabled plugins will be loaded (include_once) from your main page, and also their functionality, so each plugin can call each other's as well.