I am using ZF2 and I am wondering how to re-direct to an external URL.
This is what I have tried:
$this->redirect()->toUrl('http://www.example.com' ,
[
'access_code' => '12345'
]
);
Unfortunately it does not work.
The other thought was to simple use something like:
header('Location: http://www.example.com/12345');
EDIT:
This is being done from my controller, here is the controller code:
use Application\Library\Http\GetHttpInterface;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class GamesController extends AbstractActionController
{
public function __construct(
//factories
) {
//objects
}
public function redirectAction()
{
$this->redirect()->toUrl("http://www.example.com");
echo "here";
}
}
You just missed the return in your instruction. It should be like this:
return $this->redirect()->toUrl('http://www.example.com/12345');
Related
I have three urls:
localhost:8000/oc/online-marketing/ppc
localhost:8000/websystems/online-marketing/ppc
localhost:8000/all/online-marketing/ppc
and I need dynamic settings for each URL.
Before that, I've used to have the route.php like this:
Route::get('oc/online-marketing/ppc', function()
{
$users = User::where('client_id', 1)->get();
return View::make('users')->with('users', $users);
});
But I must set dynamic url like this: Route::get('{project}/{module}/{submodule}', ... );
where project is oc or websystems or all
The module is online-marketing
The submodule is ppc
The project name oc, websystems or all could be in table named users
How can I achieve that by using controllers?
You may try something like foillowing, declare the Route like this:
Route::get('{project}/{module}/{submodule}', array('as' => 'mycontroller.project', 'uses' => 'MyController#project'));
Create the Controller:
class MyController extends BaseController {
public function project($project, $module, $submodule)
{
//...
}
}
Here's something to get you started with ...
In your routes.php file, you can have something like:
Route::get('/{clientID}', array('uses' => 'SomeController#someFunction'));
And in the SomeController.php file:
public function someFunction($clientID)
{
$users = User::where('client_id', $clientID)->get();
return View::make('users')->with('users', $users);
}
For more information, refer to http://laravel.com/docs/routing#route-parameters
It looks like it has been touched several times already, but i still can't get it work. I set up an JSON-RPC server in a separate module, it works fine. Its functionality is in a new class Rpcapi. Now I want reuse DB related functions that already implemented in another module from that class. According to ZF2 docs my Rpcapi class has to be ServiceLocator-aware and it looks like I made it that way. Unfortunatelly still can't get it working. Please help keeping in mind that I'm new with ZF2 :)
Rpccontroller.php
namespace Rpc\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Json\Server\Server;
use Zend\Json\Server\Smd;
use Rpc\Model\Rpcapi;
class RpcController extends AbstractActionController
{
public function indexAction()
{
header('Content-Type: application/json');
$jsonrpc = new Server();
$jsonrpc->setClass(new Rpcapi);
$jsonrpc->getRequest()->setVersion(Server::VERSION_2);
if ($this->getRequest()->getMethod() == "GET") {
$smd = $jsonrpc->getServiceMap()->setEnvelope(Smd::ENV_JSONRPC_2);
echo $smd;
} else {
$jsonrpc->handle();
}
}
}
module.config.php for Rpc module
'service_manager' => array(
'invokables' => array(
'rpcapi' => 'Search\Model\SiteTable',
),
),
Rpcapi.php
namespace Rpc\Model;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class Rpcapi implements ServiceLocatorAwareInterface
{
protected $services;
protected $siteTable;
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->services = $serviceLocator;
}
public function getServiceLocator()
{
return $this->services;
}
public function getSiteTable()
{
if (!$this->siteTable) {
$sm = $this->getServiceLocator();
$this->siteTable = $sm->get('rpcapi');
}
return $this->siteTable;
}
/**
* Returns list of all sites
*
*
* #return array
*/
public function getAllSites()
{
$results = $this->getSiteTable()->fetchAll();
$r = array ('1' => '1', '2' => 2); //Just to return something for now
return $r;
}
}
All I could get out is: Fatal error: Call to a member function get() on a non-object in /var/www/html/AmeriFluxZF2/module/Rpc/src/Rpc/Model/Rpcapi.php on line 28. Line 28 is:
$this->siteTable = $sm->get('rpcapi');
Any help is much appreciated!
Making the class service locator aware tells the ZF2 that the service locator should be injected into your class upon instantiation. However, you still need to use the service locator to instantiate this class, rather than creating an instance of it yourself, or this will never happen.
Your probably want to add a new entry to invokables for your Rpcapi class, and then grab this from the service locator instead of doing new Rpcapi in your controller.
PS: The naming of your classes is very confusing - you have an Rpcapi class, and an invokable called rpcapi, yet this invokable creates an instance of a completely different class?
If you want serviceLocator to be injected by the service manager in your Rpcapi, you must get it via the service manager itself :
'service_manager' => array(
'invokables' => array(
'rpcapi' => 'Search\Model\SiteTable',
'Rpc\Model\Rpcapi' => 'Rpc\Model\Rpcapi',
),
),
the action :
public function indexAction()
{
header('Content-Type: application/json');
$jsonrpc = new Server();
$jsonrpc->setClass($this->getServiceLocator()->get('Rpc\Model\Rpcapi'));
$jsonrpc->getRequest()->setVersion(Server::VERSION_2);
if ($this->getRequest()->getMethod() == "GET") {
$smd = $jsonrpc->getServiceMap()->setEnvelope(Smd::ENV_JSONRPC_2);
echo $smd;
} else {
$jsonrpc->handle();
}
}
And this is where you can see that your 'rcpai' name for SiteTable is not a good choice... ;)
guys,
At this point i am close to start pulling hair out of my head. I don't find a way to achieve this.
I have a custom class that belongs to a custom folder i created under my WebServices Module src folder. I need to be able to instantiate this class from inside another module/controller but when i do that and dump the services member it contains null.
How can i have the service manager accesible from inside my ApiAuthentication class.
Any help will be appreciated. Thanks
<?php
namespace WebServices\Services;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
class ApiAuthenticationService extends \Zend\Soap\Client implements ServiceLocatorAwareInterface{
public $services;
function __construct($options = null){
parent::__construct('http://tinysoa.local/soap/security/api_authentication?wsdl',$options);
}
public function setServiceLocator(ServiceLocatorInterface $locator)
{
$this->services = $locator;
}
public function getServiceLocator()
{
return $this->services;
}
}
When i call this from inside another module/controller it dumps a null value:
class IndexController extends AbstractActionController
{
public function indexAction()
{
$a = new \WebServices\Services\ApiAuthenticationService();
var_dump($a->services);
Responding with my own answer to add-on to Adrian's, and the question you asked in response.
If your service has dependencies of it's own, you just use a factory instead of going the invokable route.
Say your service needs a cache adapter and database adapter. Also imagine that it can optionally be configured with some other service (FooService, below):
<?php
public function getServiceConfig()
{
return array(
'factories' => array(
'my_service' => function($sm){
$cache = $sm->get('Cache');
$dbAdapter = $sm->get('DefaultDbAdapter');
$fooService = $sm->get('FooService');
// instantiate your service with required dependencies
$mySvc = new \My\Shiny\Service($cache, $dbAdapter);
// inject an optional dependency
$mySvc->setFooService($fooService);
// return your shiny new service
return $mySvc;
}
)
);
}
Side Note: It's generally bad design to inject the ServiceManager all over the place. You're better off managing your dependencies more explicitly, like above.
This stuff is covered quite well in the Quick Start, if you haven't already read that.
Register your Service in Service Config and access it through getServiceLocator() method in controller.
Module.php
public function getServiceConfig()
{
return array(
'invokables' => array(
'my_service' => 'WebServices\Services\ApiAuthenticationService'
)
);
}
Controller
public function indexAction()
{
$service = $this->getServiceLocator()->get('my_service');
}
Hello I am having some difficulty setting up a RESTful routing for a login controller. I keep getting hit with a status 404. Here is what I have so far. Any ideas?
In my routes:
'login' => array(
array('GET', new Route('session/login')),
array('POST', new Route('session/login'))
),
And in my sessions controller I have:
class Controller_Session extends Controller_Template {
public function get_login(){
return View::forge('session/login');
}
public function post_login() {
return View::forge('session/login',$data);
}
}
Try it using the default routing and the Rest controller.
class Controller_Session extends Controller_Rest
{...}
Delete the routes you set up and try accessing the Controller using {url}/session/login
basically delete all routes you have created.
Then create a controller session.php:
class Controller_Session extends Controller_Rest
//class Controller_Session extends Controller_Hybrid
{
public function get_login()
{
return View::forge('session/login');
}
public function post_login()
{
return View::forge('session/login',$data);
}
}
You can extend Controller_Hybrid, if you want to access to both rest and non-rest methods.
Now try with jquery to access url: '/session/login'
It should work!
Good luck
It was an Apache error. The mod rewrite module was not activated on a Debian Based OS
Is there any possible way to translate strings in controllers instead of view?
Right now, in my controllers, if I pass strings like :
public function indexAction() {
return array('message' => 'example message');
}
It will be translated in index.phtml
<?php print $this->translate($message);?>
It works well, but poeditor unable to find strings from controller files
Guess it would be cool if I can use something like :
public function indexAction() {
return array('message' => $view->translate('example message'));
}
in controllers
Thanks in advance for help
To use view helper in controller, you can use 'getServiceLocator'
$helper = $this->getServiceLocator()->get('ViewHelperManager')->get('helperName');
Either you can use php getText function ___('my custom message') and add "_" as sources keyword in poedit (in catalog properties) so poedit will filter strings from controller. eg:
array('message' => _('my custom message'));
And as per your code, you can use helper directly like this
$translate = $this->getServiceLocator()->get('ViewHelperManager')->get('translate');
array('message' => $translate('my custom message'));
You should not use the view's plugin manager to get to the translator helper. Grab the translator like I have explained here already.
A copy/paste of that post:
Translation is done via a Translator. The translator is an object and injected for example in a view helper, so if you call that view helper, it uses the translator to translate your strings. For this answer I assume you have configured the translator just the same as the skeleton application.
The best way is to use the factory to inject this as a dependency into your controller. The controller config:
'controllers' => array(
'factories' => array(
'my-controller' => function($sm) {
$translator = $sm->getServiceLocator()->get('translator');
$controller = new MyModule\Controller\FooController($translator);
}
)
)
And the controller itself:
namespace MyModule;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\I18n\Translator\Translator;
class FooController extends AbstractActionController
{
protected $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
}
}
An alternative is to pull the translator from the service manager in your action, but this is less flexible, less testable and harder to maintain:
public function fooAction()
{
$translator = $this->getServiceManager()->get('translator');
}
In both cases you can use $translator->translate('foo bar baz') to translate your strings.
I use for that purpose a simple plugin. Then in controller you can do $this->translate('example message');
class Translate extends AbstractPlugin {
private $translator;
public function __construct(PluginManager $pm) {
$this->translator = $pm->getServiceLocator()->get('Translator');
}
public function __invoke($message, $textDomain = 'default', $locale = null) {
return $this->translator->translate($message, $textDomain, $locale);
}
}