Symfony2 $request->query->get() returning null value - symfony-2.3

I'm new to Symfony2.I am understanding the framework.
I try to access the get parameters of my request using Symfony2.
But it is returning null when I access them like
$name = $request->query->get('name');
echo $name;
My code for controller is
namespace abc\myBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
public function getnameAction()
{
$request = $this->get('request');
echo $request->getMethod();
$name = $request->query->get('name');
echo $name. "---";
}
my routing file is as follows:
abcmy_newpage:
pattern: /new/{name}
defaults: { _controller: abcmyBundle:new:getname }
When I run the URL
http://dashboardsmf.iiit.ac.in/web/app_dev.php/new/India
I get the method name
"GET"
corresponding to echo statement " echo $request->getMethod(); ".
But I get the null/"" blank value for the echo statement
echo $name. "---";
I dont know where I have mistaken.
Please help me.Thanks in advance.

Try to change you action to this:
public function getnameAction($name) {
echo $name;
}
Symfony binds parameters defined in routing file to parameter names in action method (see Symfony book chapter on controller parameters for more details).
If you want to use $request->query to get your parameters it should be passed like this: http://http://dashboardsmf.iiit.ac.in/web/app_dev.php/new?name=India.

Related

Slim 4 get all routes into a controller without $app

I need to get all registed routes to work with into a controller.
In slim 3 it was possible to get the router with
$router = $container->get('router');
$routes = $router->getRoutes();
With $app it is easy $routes = $app->getRouteCollector()->getRoutes();
Any ideas?
If you use PHP-DI you could add a container definition and inject the object via constructor injection.
Example:
<?php
// config/container.php
use Slim\App;
use Slim\Factory\AppFactory;
use Slim\Interfaces\RouteCollectorInterface;
// ...
return [
App::class => function (ContainerInterface $container) {
AppFactory::setContainer($container);
return AppFactory::create();
},
RouteCollectorInterface::class => function (ContainerInterface $container) {
return $container->get(App::class)->getRouteCollector();
},
// ...
];
The action class:
<?php
namespace App\Action\Home;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Response;
use Slim\Http\ServerRequest;
use Slim\Interfaces\RouteCollectorInterface;
final class HomeAction
{
/**
* #var RouteCollectorInterface
*/
private $routeCollector;
public function __construct(RouteCollectorInterface $routeCollector)
{
$this->routeCollector = $routeCollector;
}
public function __invoke(ServerRequest $request, Response $response): ResponseInterface
{
$routes = $this->routeCollector->getRoutes();
// ...
}
}
This will display basic information about all routes in your app in SlimPHP 4:
$app->get('/tests/get-routes/', function ($request, $response, $args) use ($app) {
$routes = $app->getRouteCollector()->getRoutes();
foreach ($routes as $route) {
echo $route->getIdentifier() . " → ";
echo ($route->getName() ?? "(unnamed)") . " → ";
echo $route->getPattern();
echo "<br><br>";
}
return $response;
});
From there, one can use something like this to get the URL for a given route:
$routeParser = \Slim\Routing\RouteContext::fromRequest($request)->getRouteParser();
$path = $routeParser->urlFor($nameofroute, $data, $queryParams);
With the following caveats:
this will only work for named routes;
this will only work if the required route parameters are provided -- and there's no method to check whether a route takes mandatory or optional route parameters.
there's no method to get the URL for an unnamed route.

GET url parameters Codeigniter

There is one view, and one controller, it is necessary, with the use of the get parameter, to change the path by the link. So in the end it turned out like this:
Upcoming -> http://mywebsite.com/tournaments?type=upcoming
Finished -> http://mywebsite.com/tournaments?type=finished
Here is the controller which will process your GET variable
Controller
<?php
class Demo_controller extends CI_Controller
{
public function demo($id)
{
echo $id; //the id which you will get in URL string
}
}
?>
View File where you will pass the Variable
<?php
$id = "1";//dyanmic id which will be pased with the form
echo form_open('demo/demos-function'.$id);
//in between your code
echo form_close
?>
Routes
Inside routes.php
$route['demo/demos-function/(:any)] = Demo_controller/demo/$1
No need to change the $1 it will explain the routes that it is having URL paramater.
So Ultimately Your link will be
https://localhost/demo/demos-function/1
https://localhost/demo/demos-function/2
In Codeigniter Url like this :
Upcoming -> http://mywebsite.com/controller/method/upcoming
Finished -> http://mywebsite.com/controller/method/finished
public function method_name()
{
$type = $this->uri->segment(3); // third level value of URL - Segment(1) is controller and followed by method and query string
if($type == 'upcoming')
{
echo $type;
$this->load->view('upcoming');
}
else
{
echo $type;
$this->load->view('finished');
}
}

In Magento 2 how can I retrieve a container from layout?

How can I retrieve a container from the layout programmatically?
I would like to be able to do something like the following...
$container = $layout->getContainer('name');
$container->setAttribute('htmlClass', 'class');
So I'll be that guy, try your best not to use the object manager directly. To use or not to use the ObjectManager directly?
<?= $block->getLayout()->renderNonCachedElement('top-right-wrapper'); ?>
-or-
<?= $this->getLayout()->renderNonCachedElement('top-right-wrapper'); ?>
Both above will work but using $this is discouraged. Magento 2 Templates: Use $block or $this?
With little debugging in core I have found that there is a method which
returns any container,block or UIcomponent html using name as parameter.
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$layoutObj = $objectManager->get('Magento\Framework\View\Layout');
$html = $layoutObj->renderNonCachedElement('top-right-wrapper');
echo $html;
* Replace top-right-wrapper with your block,container or UIcomponent name.

Slim Framework - Using a GET route

I'm absolutely new to Slim Framework. I'm working on an Webservice that should provide an interface between an Android App and a Web-Application. I used the Slim Documentation to make my first steps and now I want to create a simple GET route, to receive information from the App. Here is what I have so far:
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/:name', function ($name) use($app) {
$name_outside = $name;
echo $name;
});
$app->run();
echo $name_outside;
I need to access the variable :name outside the function, but what I get is nothing. What I am doing wrong here?
Btw: I know that GET-routes usually are used to list existing resources, but for my simple case, I decided to use it that way.
Fix your code to hold name as args parameters ,then you can get in in Your function
require 'Slim/Slim.php';
\Slim\Slim::registerAutoloader();
$name_outside = '';
$app = new \Slim\Slim();
$app->get('/session/program_name/{name}', function ($args) use($app) {
$name_outside = $args['name'];
echo $args['name'];
});
$app->run();
To acess the $name_outside inside the function context you pass it.
$app->get('/session/program_name/:name', function ($name) use($app, &$name_outside) {
$name_outside = $name;
echo $name;
});
But perhaps you're using Slim in a wrong way. Why you have to access the variable outside of your route?
No code is execyted after the run() call. That's the way slim works, try to put a die where you're echo the variable, it isn't reachable.
You shouldn't have the need to access the context of the route outside of it this way. To transform a request or a response you use middlewares with the hooks.

Sending a SOAP message with PHP

what I'm trying to do is send a load of values captured from a form to a CRM system with SOAP and PHP. I've been reading up on SOAP for a while and I don't understand how to go about doing so, does anybody else know?
In order to do this it might be easiest to download a simple soap toolkit like 'NuSOAP' from sourceforge.
And then you would code something like the following (example submission of ISBN number):
<?php
// include the SOAP classes
require_once('nusoap.php');
// define parameter array (ISBN number)
$param = array('isbn'=>'0385503954');
// define path to server application
$serverpath ='http://services.xmethods.net:80/soap/servlet/rpcrouter';
//define method namespace
$namespace="urn:xmethods-BNPriceCheck";
// create client object
$client = new soapclient($serverpath);
// make the call
$price = $client->call('getPrice',$param,$namespace);
// if a fault occurred, output error info
if (isset($fault)) {
print "Error: ". $fault;
}
else if ($price == -1) {
print "The book is not in the database.";
} else {
// otherwise output the result
print "The price of book number ". $param[isbn] ." is $". $price;
}
// kill object
unset($client);
?>
This code snippet was taken directly from, which is also a good resource to view
http://developer.apple.com/internet/webservices/soapphp.html
Hope this helps.
You probably found a solution since then - but maybe the following helps someone else browsing for this:
soap-server.php:
<?php
class MySoapServer {
public function getMessage ()
{
return "Hello world!";
}
public function add ($n1,$n2)
{
return $n1+n2;
}
}
$option = array ('uri' => 'http://example.org/stacky/soap-server');
$server = new SoapServer(null,$option);
$server->setClass('MySoapServer');
$server->handle();
?>
and soap-client.php
<?php
$options = array ('uri' => 'http://example.org/stacky/soap-server',
'location' => 'http://localhost/soap-server.php');
$client = new SoapClient(null,$options);
echo $client ->getMessage();
echo "<br>";
echo $client ->add(41,1);
?>