laravel routes.php dynamic url - url

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

Related

ZF2 - How to re-direct to External URL

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');

RESTful routing in FuelPHP

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

Zend Framework 2 Custom elements using ServiceManager not work

I want create a custom element and use the short name for add the element into Form, using the new ServiceManager tecnique for ZF2 V.2.1+
I am try to copy the same sample of the zend documentation step to step but it not works.
When I use the service writting the short name, it raises a exception because service not found:
Zend\ServiceManager\Exception\ServiceNotFoundException
File:
Zend\ServiceManager\ServiceManager.php:456
Message:
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Test
I think I have all classes identically, see follows
This is my custom element:
namespace SecureDraw\Form\Element;
use Zend\Form\Element\Text;
class ProvaElement extends Text {
protected $hola;
public function hola(){
return 'hola';
}
}
This is my Module.php I have my invokable service be able to use short name:
class Module implements FormElementProviderInterface {
//Rest of class
public function getFormElementConfig() {
return array(
'invokables' => array(
'Test' => 'SecureDraw\Form\Element\ProvaElement'
)
);
}
}
In my form I use for add the element, the commented line works ok, but with short name not works:
$this->add(array(
'name' => 'prova',
//'type' => 'SecureDraw\Form\Element\ProvaElement',
'type' => 'Test', //Fail
));
In my action:
$formManager = $this->serviceLocator->get('FormElementManager');
$form = $formManager->get('SecureDraw\Form\UserForm');
$prova = $form->get('prova');
echo $prova->hola();
The problem is that the elements created via FormElementManager have to be created into init method instead __Construct method how it can see in this page.
The zend documentation is badly explained
Workaround:
In your own module, create the following two files:
FormElementManagerConfig with the invokables short names of your custom form elements;
Subclass Form\Factory and override getFormElementManager and pass the config to the FormElementManager constructor;
You then use your own Factory to create your Form, like this (you can pass a very rudimentary, e.g. empty array, or a more or less full-fledged $spec to $factory->createForm()):
$factory = new Factory();
$spec = array();
$form = $factory->createForm($spec);
FormElementManagerConfig.php:
class FormElementManagerConfig implements ConfigInterface
{
protected $invokables = array(
'anything' => 'MyModule\Form\Element\Anything',
);
public function configureServiceManager(ServiceManager $serviceManager)
{
foreach ($this->invokables as $name => $service) {
$serviceManager->setInvokableClass($name, $service);
}
}
}
MyFactory.php:
class Factory extends \Zend\Form\Factory
{
public function getFormElementManager()
{
if ($this->formElementManager === null) {
$this->setFormElementManager(new FormElementManager(new FormElementManagerConfig()));
}
return $this->formElementManager;
}
}

How to use translate helper in controllers in zend framework 2

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);
}
}

Symfony's Jobeet can't find the index page?

I'm in the day 10 of Symfony's Jobeet Tutorial. Everything worked good, but when I tried to go to the index page: http://localhost:9090/frontend_dev.php
I got the following message:
sfPatternRouting Match route "job" (/job.:sf_format) for /job with parameters array ( 'module' => 'job', 'action' => 'index', 'sf_format' => 'html',)
2 Info sfFrontWebController Action "job/index" does not exist
3 Error sfError404Exception Action "job/index" does not exist.
(I still have a backup of day 9, and the index page works fine).
Any suggestions?
I guess you have replaced the methods in app/modules/job/actions.class.php with what you found on day 10, instead of simply adding them. There must be an executeIndex() method in this file if you want to get something in /job
Yeah, it's something like this:
class jobActions extends sfActions
{
public function executeIndex(sfWebRequest $request)
{
$this->categories = Doctrine_Core::getTable('JobeetCategory')->getWithJobs();
}
public function executeShow(sfWebRequest $request)
{
$this->job = $this->getRoute()->getObject();
}
public function executeNew(sfWebRequest $request)
{
...
}
...
}
I also had overwrite it. Yeah so executeIndex and executeShow are important for "index" and "show". ;)

Resources