Can Magento be integrated with CakePHP? - cakephp

Can Magento be integrated with CakePHP?
If my site is developed in CakePHP. Can I do the product module including shopping cart in Magento?

Yes, it can. For example:
require_once 'app/Mage.php';
umask(0);
Mage::app();
Mage::getSingleton('core/session', array('name'=>'frontend'));
$cart = Mage::helper('checkout/cart')->getCart()->getItemsCount();
echo 'Items count: ' . $cart;
Look at these articles:
http://www.exploremagento.com/magento/run-magento-code-outside-of-magento.php
http://blog.chapagain.com.np/magento-how-to-run-magento-code-in-an-external-website/

can make the Mage class working for me in pure php code with the above example. But you know Cakephp has its own routing mechanism. I have magento installed in the root and trying to add another application built with cakephp - that application has its own data structure & database (mainly be used for custom reporting and some tracking stuffs) but will share some data from magento (that is the main site)

I managed to do a hack (and there where no other way to do that).
The hack is you need to put the "function __()" inside in line 93 of Magento app/code/core/Mage/Core/functions.php
if (!function_exists('__')) {
function _ () { .... }
}
and you need to do the same for "DS" in app/Mage.php line 23
if(!defined('DS')) {}
then you can just use the Mage class and do all the operation to Megento.

Related

Check if event is coming from a model inside a plugin?

I need all models inside a custom CakePHP plugin to use a database prefix. I'm trying to use an event, as suggested by #lorenzo.
EventManager::instance()->on('Model.initialize', function ($event) {
$instance = $event->subject();
$instance->table('prefix_' . $instance->table());
});
I'm getting several callbacks from my plugin model as well as DebugKit models, and potentially it could be other models in the application.
Is there a way to tell if a given $event is coming from within a plugin?
I have checked $event->getSubject() and it contains the corresponding Table class. The only feasible way I could come up with is to check some properties for the plugin name.
$event->getSubject()->getRegistryAlias() is ExamplePlugin.Posts
$event->getSubject()->getEntityClass() is ExamplePlugin\Model\Entity\Post
I could check if either starts with ExamplePlugin. Is there a better way?
The fact that basically any PHP namespace can be a plugin means you could do something like that:
EventManager::instance()->on('Model.initialize', function (\Cake\Event\EventInterface $event) {
/** #var \Cake\ORM\Table $object */
$object = $event->getSubject();
$tableClassName = get_class($object);
$isApp = str_starts_with($tableClassName, 'App');
});
Because your main app's namespace will always begin with App
This of course wouldn't distinguish between your private plugins which are located in plugins and plugins which are installed via composer and therefore live in the vendor directory.
But you could introduce a name prefix to all your private plugins so you can easily distinguish them from any other plugins.

How To Programmatically Set Up Wagtail Root Page For Tests Utilizing The StaticLiveServerTestCase Suite

This question is similar to another question here on stack overflow .
I am in the process of adding tests to my wagtail site utilizing Django's StaticLiveServerTestCase. Below is an example of the code base I have at hand:
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
So when I run this test with python manage.py test, the test fails because I there is a 500 error. Please recall that I am using wagtail and NOT Vanilla Django alone. I am also using Django's Site framework as opposed to Wagtail's Site framework as allauth only allows for usage with Django's Site framework.
After applying the #override_settings(DEBUG=True) to the test like this:
#override_settings(DEBUG=True)
class ExampleTest(StaticLiveServerTestCase):
def setUp(self):
self.browser = webdriver.Chrome()
def test_example_test(self):
self.assertContains("Contact Page", self.browser.content)
[...]
The test still fails as the page that is being loaded is the wagtail default page.
My question is, how do I set up another page as the root/default wagtail page such that when a request to localhost:8000 [or any other port number given by the test server] is being made to the home page (i.e. http://localhost:8000/), I see that new page instead of the wagtail default page?
Thanks.
Since StaticLiveServerTestCase creates a new [temporary] "test" database [including running migrations and migrate], wagtail literally resets all sites and pages back to it's initial state after initial wagtail start [mysite] command.
This means that if you have any other Page that you would like to be the root page, you will have to hard code the instruction to do that.
Below is a way in which this can be achieved.
It is advisable to set these instructions within the setUpClass method of a class — usually a class Main() class where other test classes can inherit from; thereby encouraging D.R.Y.
class Main(StaticLiveServerTestCase):
#classmethod
def setUpClass(cls):
super(Main, cls).setUpClass()
cls.root = Page.objects.get(id=1).specific
cls.new_default_home_page = Index(
title="New Home Page Index",
slug="index",
)
cls.root.add_child(instance=cls.new_default_home_page)
cls.site = Site.objects.get(id=1)
cls.site.root_page = cls.new_default_home_page
cls.site.save()
cls.browser = Chrome()
Now my test classes (wherever they are) can inherit from this class and get the entire new home page setup instantly. For example:
# ExampleTest() inherits from Main() for ease of Wagtail Page setup: avoiding repetition of setUpClass().
class ExampleTest(Main):
def test_example_test(self):
self.assertContains("Contact Page", self.browser.title)
[...]
Hope this helps someone out there someday.
THIS SOLUTION IS VALID FOR: wagtail==2.7.4. anything above this version isn't guaranteed to work as wagtail's code base dictates. However, it's very unlikely that this wouldn't work.

How to save doctrine database schema in a Symfony controller?

I'm using Symfony 2.3 and Doctrine 2 and i need that an user save the schema of a Doctrine Database to a file (*.sql). I need it into an action method and then send the file to the user
You need to execute following command:
.app/console doctrine:schema:create --dump-sql >schema.sql
And here's the answer how to run Command from Controller: How can I run symfony 2 run command from controller
Just to get you started, this should work in concept. I didn't get to run it, so I assume it might need a little tweaks from your side.
<?php
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Doctrine\Bundle\DoctrineBundle\Command\Proxy\CreateSchemaDoctrineCommand;
// your controller
public function myAction()
{
$command = new CreateSchemaDoctrineCommand();
$command->setContainer($this->container);
$input = new ArrayInput(array('--dump-sql' => true));
$output = new NullOutput();
$schema = $command->run($input, $output); //This is your schema
// Write it to a file if you want
file_put_contents('path/to/schema.sql', $schema);
}
References:
Similar question
PHP file_put_contents()
Symfony2 FileSystem

Laravel X AngularJS - prefix/trailing slash

As you know Laravel4 omits the trailing slashes from all URLS.
I've Laravel4 X AngularJS SPA (Single Page Application), and simply my current URLs looks like this:
http://localhost/en#/nglink
What I'd like to achieve is to make links looks like this:
http://localhost/en/#/nglink
So as you can see, I need a prefix slash before the AngularJS links (#/nglink), or a trailing slash after Laravel's links (http:// localhost/en). Is there anyway to achieve this using AngularJS? If not how to achieve it without editing Laravel's core files?
Well, it's possible to achieve that though either AngularJS or Laravel or http server side, but it's better & easier to be done through Laravel itself since we can just override the required classes (URLGenerator mainly) without touching core files, and while keeping the code (server agnostic), so it could work with apache/nginx or any other server with trailing slash (that's why I preferred not to work with htaccess).
Update #1
Laravel 5 / AppServiceProvider::register()
$this->app->bindShared('url', function ($app) {
$routes = $app['router']->getRoutes();
$request = $app->rebinding('request', function ($app, $request) {
$app['url']->setRequest($request);
});
// This is your custom overridden "UrlGenerator" class
$urlGenerator = new UrlGenerator($routes, $request);
return $urlGenerator;
});

Email template not using themed version

I am using CakePHP 1.3 and the built in email features as described in the documentation. I have the html version of the template located in app/views/elements/email/html/reservation.ctp and its working as expected.
$this->Email->template = 'reservation'; // no '.ctp'
I also have a theme setup and most of the themed files are correctly overriding the default files. My problem is the themed email template is not being used when called from the themed site, its still using the email template file in the default path.
The default is at: app/views/elements/email/html/reservation.ctp
The theme is at: app/views/themed/myTheme/elements/email/html/reservation.ctp
Should the email template assignment automatically work with themes without the need for hard coding a path or is there another solution? Anyone else have this issue?
in cakephp when you want to create email template. Lets suppose we want to create an Html email. and email config is configured.
Views[File Structure]:
1) your content email with variables should be located in View/Emails/html [reservation.ctp]
2) your template should be located in View/Layouts/Emails/html [default.ctp OR any new template you have made]
controllers:
Note: some people think when you write an action(in controller) you have to write a view for it. In this case (for sending email) is completely wrong. only if you want to show the result which email sent successfully or not then is fine.
lets say ReserveController ;) and sendReservationEmail
function sendReservationEmail( $to, $from,$subject ,$template, $variables=array()){
$Email = new CakeEmail();
$Email->config('smtp')
->viewVars($variables)
->emailFormat('html')
->template($template['page'], $template['layout']) //'reservation', 'default'
->from($from) //'me#example.com' => 'My Site'
->to($to) //'you#example.com'
->subject($subject) //'Resevation'
->send();
}
Views (View/Emails/html/reservation.ctp):
Dear $this->viewVars['name'];
Welcome to our restaurant .....

Resources