Accessing services directory when setting up Zend AMF in Codeigniter - apache-flex

I followed the instructions in this tutorial to set up Zend AMF as a way of passing data from my flash app to my PHP app:
http://codeigniter.com/forums/viewthread/180414/
So I have the directory structure and everything as described there. This is my gateway controller:
class Gateway extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->library('zend');
//root_folder + application + controllers + amf + services
define('SERVICES_FOLDER', APPPATH.'controllers/amf/services/');
}
public function index()
{
$server = new Zend_Amf_Server();
$server->setProduction(false);
//$server->addFunction('testservice');
$server->addDirectory(SERVICES_FOLDER);
echo $server->handle();
}
}
And the APPPATH is /application/ so the path defined by SERVIES_FOLDER is "/application/controllers/amf/services" which is where my file 'testservice.php' sits.
When I try and connect to that service in flash:
var gateway:String = "http://mysite.com/amf/gateway";
con.connect(gateway);
con.call("Testservice.getMessage", new Responder(onResult, onFault));
It calls the onFault method and displays the error:
Plugin by name 'Testservice' was not found in the registry;
Which makes me think that the addDirectory() line in Gateway.php was the problem somehow. Interestingly, I also cannot access the testservice function through a URL, ie by going to mysite.com/amf/services/testservice.
Any thoughts on what might be going on here?

Figured it out, sort of.
Instead of using the addDirectory method which I was having no luck with, I used the setClass method and created another class within the gateway.php file that has the functions, and now I can connect to those functions from flash.

I had an issue with this when using parent::__construct() in my service controllers. Once I removed that, the problem went away.

Related

Symfony VichUploaderBundle autowiring on DownloadHandler in a controller

I'm using Symfony 4 with VichUploaderBundle 1.9 and I'm having hard time injecting the DownloadHandler service in my controller in order to send file to the client.
I'm also using HashidsBundle in order to convert my entity ID to something like jFaJ in my URLs.
As stated in the VichUploaderBundle documentation, I'm injecting the service in my controller like this :
public function download(Wallpaper $wallpaper, DownloadHandler $downloadHandler)
{
return $downloadHandler->downloadObject($wallpaper->getMedia(), 'uploadedFile');
}
Here is the error I'm having:
Argument 2 passed to App\Controller\WallpapersController::download()
must be an instance of Vich\UploaderBundle\Handler\DownloadHandler,
integer given, called in
/mnt/c/Users/user/Documents/Dev/symfony/vendor/symfony/http-kernel/HttpKernel.php
on line 151
I also tried to manually call the service by adding the following line in my controller:
$this->get('vich_uploader.download_handler');
But it's still not working, I have this error now:
Service "vich_uploader.download_handler" not found: even though it exists in the app's container, the container inside "App\Controller\WallpapersController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.
You can return the file using BinaryFileResponse.
public function download(Wallpaper $wallpaper): BinaryFileResponse
{
$file = new BinaryFileResponse($wallpaper->getMedia());
return $file;
}
For more info, check
https://github.com/aythanztdev/prbtcnccd/blob/master/src/Controller/MediaObject/ShowMediaObjectAction.php

Is there a way to prevent multiple executions of controller method in Symfony 4?

I have a service which I use both from a custom command and an HTML page. I want to prevent multiple executions of the the service in parallel. For the command there is the Lock component that does that. But is it possible to achieve the same thing for a controller method ?
The lock component doesn't work if the service is called from a controller:
$store = new FlockStore(sys_get_temp_dir());
$factory = new Factory($store);
$lock = $factory->createLock('MY_SERVICE');
I wanted to avoid calling the command from the controller (that's why I created a service) mainly because the service doesn't have the same output for the HTML page and the CLI.
Inject the lock Factory into your service directly instead of creating the lock in the command AND in the controller.
First you have to install Lock Component:
composer require symfony/lock
Then, for example, you can declare your service like this:
use Symfony\Component\Lock\Factory as LockFactory;
class MyService {
private $lock;
public function __construct(LockFactory $lockFactory) {
$this->lock = $lockFactory->createLock('LOCK_KEY');
}
public function doWork() {
$this->lock->acquire();
try {
// DO THINGS
} finally {
$this->lock->release();
}
}
}
I said:
The lock component doesn't work if the service is called from a controller:
Actually the issue I had was the Symfony built-in dev server which is single-threaded, so requests can't be executed in parallel, while the CLI PHP is multi-threaded. I couldn't run the script in parallel through the dev server, request were queued, service script was never locked.
The lock component is working the same whether it's called from a command or a controller.
Using the lock like this in the service works fine:
use Symfony\Component\Lock\Factory;
use Symfony\Component\Lock\Store\FlockStore;
$store = new FlockStore(sys_get_temp_dir());
$factory = new Factory($store);
$lock = $factory->createLock('LOCK_KEY');
if ($lock->acquire()) {
//some locked code
$lock->release();
}

library module upgrade to Glide 4, where should the AppGlideModule be put in

In library module to upgrade to Glide 4.9.0.
api "com.github.bumptech.glide:glide:4.9.0"
api "com.github.bumptech.glide:annotations:4.9.0"
annotationProcessor "com.github.bumptech.glide:compiler:4.9.0"
and having a kotlin extension
fun ImageView.loadImg(imageUrl: String) {
// 4.+ code
var requestOptions : RequestOptions = RequestOptions()
.placeholder(ColorDrawable(Color.LTGRAY))
.diskCacheStrategy(DiskCacheStrategy.ALL)
if (!TextUtils.isEmpty(imageUrl)) {
Glide.with(context)
.setDefaultRequestOptions(requestOptions) // or use .apply(requestOptions) but after the .load()
.asBitmap()
.load(imageUrl)
.into(this)
}
}
but it crashes
java.lang.AbstractMethodError: abstract method "void com.bumptech.glide.module.RegistersComponents.registerComponents(android.content.Context, com.bumptech.glide.Glide, com.bumptech.glide.Registry)"
at com.bumptech.glide.Glide.initializeGlide(Glide.java:270)
at com.bumptech.glide.Glide.initializeGlide(Glide.java:223)
at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:184)
at com.bumptech.glide.Glide.get(Glide.java:168)
at com.bumptech.glide.Glide.getRetriever(Glide.java:689)
at com.bumptech.glide.Glide.with(Glide.java:716)
at com.common.extentions.ExtensionsKt.loadImg(Extensions.kt:44)
After adding
#GlideModule
class TheAppGlideModule : AppGlideModule() {
override fun isManifestParsingEnabled(): Boolean {
return false
}
}
to the library module does not help, or adding it to hosting app only does not work either,
but after adding it to both the library module and the hosting app the crash goes away.
according to documentation https://bumptech.github.io/glide/doc/generatedapi.html,
isnt it that it not supposed to have this class defined in the library module?
anyone has same experience?
* For now the API is only generated when a properly annotated AppGlideModule is found.
* There can only be one AppGlideModule per application.
* As a result it’s not possible to generate the API for a library without precluding any application
* that uses the library from using the generated API.
Resolved, it has missed
api "com.github.bumptech.glide:annotations:$versions.glide"
in the application side (not sure why adding single one in the module did not work and why with both it worked, maybe didnt do clear/rebuild after change?)

Examples to use netflix-feign outside of spring-boot

I m looking for a example how to use netflix-feign from a NO-spring-boot app.
I have a existing SpringMVC (4.2) webapp. Now i build some "microservices" with Spring-boot + (eureka, feign) and i want to use those as backend serivces from the webapp.
thanks in advance
In the github repo you can see a few examples to use feign-client:
https://github.com/OpenFeign/feign
Basically, you would need to create an interface annotated with feign annotations and not with the Spring, one example would be (you can see much more in the github page):
public interface YourClient {
#RequestLine("POST /")
#Headers("Content-Type: application/json")
Response getSomething(#Param("id") String id);
}
Then to instantiate your feign client it's need to use its builder Feign. It's easy, and configurable:
YourClient yourClient = Feign.builder()
.decoder(new GsonDecoder()) // you could use Gson, Jackson,...
.target(YourClient .class, "https://your.url");
Then you can use it just invoking the method you want:
yourClient.getSomething("myId");

Routing in Silex/Symfony. Providing a default route

I'm attempting to do something using Silex (which uses the Symfony routing component - so the answer may be applicable to Symfony as well)
I am adding Silex to a legacy application to provide routing but I need to respect the existing applications default implementation for loading files (which is simply to load the file from the file system form the URL specified).
edit: for clarification:
Existing file is loaded from the file system, as an include within an parent template, after a series of bootstrapping calls have been made.
What I'm finding is that in the absence of a defined route to match the legacy pages, Silex is throwing an exception.
I really need a way to provide a default (fallback) mechanism for handling those legacy pages - but my pattern has to match the entire url (not just one fragment).
Is this possible?
// Include Silex for routing
require_once(CLASS_PATH . 'Silex/silex.phar');
// Init Silex
$app = new Silex\Application();
// route for new code
// matches for new restful interface (like /category/add/mynewcategory)
$app->match('/category/{action}/{name}/', function($action, $name){
//do RESTFUL things
});
// route for legacy code (If I leave this out then Silex
// throws an exception beacuse it hasn't matched any routes
$app->match('{match_the_entire_url_including_slashes}', function($match_the_entire_url_including_slashes){
//do legacy stuff
});
$app->run();
This must be a common use case. I'm trying to provide a way to have a RESTFUL interface alongside legacy code (load /myfolder/mysubfolder/my_php_script.php)
I found the answer within the symfony cookbook...
http://symfony.com/doc/2.0/cookbook/routing/slash_in_parameter.html
$app->match('{url}', function($url){
//do legacy stuff
})->assert('url', '.+');
You can use the error handling, with something like that :
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
$app->error(function (\Exception $e) use ($app) {
if ($e instanceof NotFoundHttpException) {
return new Response('The requested page could not be found. '.$app['request']->getRequestUri(), 404);
}
$code = ($e instanceof HttpException) ? $e->getStatusCode() : 500;
return new Response('We are sorry, but something went terribly wrong.', $code);
});

Resources