How to put console command from a frontend button? - php

I am a newbie in Symfony. Anyhow I have found my way to build up a Console Command. So this command needs to be accessible from the frontend.
So by my opinion, I need to put the command to service. I have followed this link.
So this should be created. But now I don't know how to connect this service to the actual route call.
I have formed a route like this:
command:
path: /command
defaults:
_controller: AppBundle:Command:activate
requirements:
language: '%pimc.akeneo_cms.frontend.language.available%'
And I have created new controller called CommandController with just one method called activateAction().
And I don't know what to put in actiavateAction ?
Could someone help me ? Am I on a right path ?

You can follow these steps:
create the console command
create the controller with an action method and a route
create a twig template to be rendered from the controller's method action
inside the template, create a button
using ajax, on button click, make a request into that controller's method action (or another method action)
inside the method, call the console command as here is explained

If you want to run a command in your controller's action, you can use Application:
$application = new Application($this->get('kernel'));
$input = new ArrayInput(array('command' => 'your:command'));
$output = new BufferedOutput();
$application->run($input, $output);
And if you want to check the output of the command you can use $output->fetch().

Related

Can I define any route without callback function in laravel?

To register a route, I specify the route in web.php in following way,
Route::get($uri, $callback)->name($name);
Is there any way to do this without any specific callback function?
I am building a laravel website but some portion are made with perl. I need to submit a form to a url where the perl code takes over. However, as the form is used in many places I want to use the $name part as form action inside my front end codes.
Essentially what I want is something like this,
Route::get('cgi-bin/route_to_perl_program.cgi', "DO NOTHING. LET PERL DO IT JOB")->name($url_name);
If you not to define any $callback function,there is no need to define a route in web.php. As you want to use route('name') to produce that url in all your blade file,there is an alternative. You need to add this line of code in your config/app.php as follows:
perl_url => 'cgi-bin/route_to_perl_program.cgi',
And use them in your blade like as
link text
This is not your answer,this might be a solution of your specific
need.
In your $callback function to run a command to execute your perl file and get back the output and return that output as a response.,e.g:
.....
$yourPerlOutPut = exec('perl ~/your/path/yourfile.pl');
return response($yourPerlOutPut);
exec() executes the given command.
You can put your route_to_perl_program.cgi file in public folder and access yourdomain.com/route_to_perl_program.cgi. It will run. You need not put it the route.
Or try something like the following
Route::get('cgi-bin/route_to_perl_program.cgi', function(){
exec('cgi-bin/route_to_perl_program.cgi');
})->name($url_name);

No route found for "GET /Index" when generating Symfony controller

I am using generate:controller to create a new controller in my Symfony 3 application. But the route is not getting found.
Here is the input/output of the command ...
First, you need to give the controller name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post
Controller name: ApplicationSonataPageBundle:Page
Determine the format to use for the routing.
Routing format (php, xml, yml, annotation) [annotation]: yml
Determine the format to use for templating.
Template format (twig, php) [twig]:
Instead of starting with a blank controller, you can add some actions now. An action
is a PHP function or method that executes, for example, when a given route is matched.
Actions should be suffixed by Action.
New action name (press <return> to stop adding actions): IndexAction
Action route [/Index]:
Template name (optional) [ApplicationSonataPageBundle:Page:index.html.twig]:
New action name (press <return> to stop adding actions):
Summary before generation
You are going to generate a "ApplicationSonataPageBundle:Page" controller
using the "yml" format for the routing and the "twig" format
for templating
Do you confirm generation [yes]?
... and here is the content of the new controller class:
namespace Application\Sonata\PageBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class PageController extends Controller
{
public function IndexAction()
{
return $this->render('ApplicationSonataPageBundle:Page:index.html.twig', array(
// ...
));
}
}
... which is not called. I instead receive the following error in the browser:
No route found for "GET /Index"
Is there an additional step I need to do before seeing my action?
What I have tried: I have so far tried every other type of routing (xml, php, yml) without success.
There are a couple of things to check:
Make sure your routes are included inside config/routes.yml
https://symfony.com/doc/3.3/routing.html
https://symfony.com/doc/3.3/routing.html#loading-routes
https://symfony.com/doc/3.3/routing/external_resources.html
Also make sure your bundle is loaded in the kernel: app/AppKernel.php
https://symfony.com/doc/3.3/page_creation.html#bundles-configuration
Another thing I notice, your action/route is capitalized. I'm not sure if this effects things but the common practice is camelCasing so you would have indexAction rather than IndexAction
There is also a command to show routes that are available I believe it is something like php bin/console routes:debug if you run php bin/console it should show you available commands
For what it's worth: I ended up just giving up and copying the following:
_index:
path: /Index
defaults: { _controller: ApplicationSonataPageBundle:Page:Index }
... from the generated yml file into my app's main routing.yml file.

How to add a new page to the admin view of laravel framework

I have a website in laravel framework and I am trying to add a simple new static page to the admin panel. I have done the following three steps:
Add a template to the views:
app/views/admin/MessageToAll.blade.php
Add the make view code in the controller.
public function MessageToAll(){
return View::make('admin.MessageToAll');
}
Added a route in app/routes.php
Route::get('/admin/MessageToAll',array('as'=>'MessageToAll','uses'=>'AdminController#MessageToAll'));
But when I go to to domain.com/admin/MessageToAll
it gives me a 404 page not found error. Does anyone know what have I missed as I think I have completed all steps for adding this view.
Just put your new route before the /admin/ route (to test it, you want to temporarily make it the very first route in routes.php). The problem is /admin/ or some other similar route executed before your new route.
Also, if you need to just execute static view, you can use something like this (works without using a controller):
Route::get('/admin/MessageToAll', function (){
return View::make('admin.MessageToAll');
});
in routes add:
Route::get('/admin/MessageToAll','yourController#yourMethod');

In ZF2 how to make view functions to run in controller

I wanted the functionalities of view files to run in controller file also.
For example, I wanted $this->escapeHtml() which runs in view file alone to run in controller through some means like $this->...->escapeHtml()
Is this possible? Kindly help.
You need to get the ViewHelperManager and extract the EscapeHtml helper. This is a one example how to do it from the controller:
$viewHelperManager = $this->getServiceLocator()->get('ViewHelperManager');
$escapeHtml = $viewHelperManager->get('escapeHtml'); // $escapeHtml can be called as function because of its __invoke method
$escapedVal = $escapeHtml('string');
Note that it is recommended to escape and display the output in the view scripts and not in the controller.

Issue on zend dispatch loop

I'm using Zend Framework to build a website and I'm having some trouble with the dispatch loop.
Normally, Zend Framework URLs are built this way: http://www.domain.com/module/controller/action.
On my website, I'm using customized dynamic URLs which are parsed on the dispatch loop by a custom method. So, each one of these URLs, after being parsed, will execute a specific action of a specific controller and module.
I need to perform some tasks which depend on the module, controller and action that were parsed. The problem is that I'm only being able to know the parsed module, controller and action when dispatchLoopShutdown occurs. The tasks that I need to execute will set some cookies which will make changes on the output that will be sent to the browser.
But, at this point, the view has already been rendered, and the cookies that were set when dispatchLoopShutdown occurs won't change the output accordingly.
So, my question is... is there a way to force the view to be rendered again? Or a way to know what module, controller and action will be executed, before the dispatchLoopShutdown? I've also tried to accomplish this on the postDispatch but the results are the same!
Hope I could explain my problem right.
Thank you for your help.
Here is a good schema about the Zend Framework sequence.
You can know the module, controller and action before the dispatch by using a controller plugin:
<?php
class Custom_Controller_Plugin_CheckRoute extends Zend_Controller_Plugin_Abstract
{
public function preDispatch($request)
{
//Get Request
$controller = $request->controller;
$action = $request->action;
$module = $request->module;
//=> perform actions before dispatch
//Update the Request
$request->setModuleName('default')
->setControllerName('index')
->setActionName('action2');
}
}
?>
I had the same problem. It was solved by Zend_Controller_Plugin_ActionStack. I added some action where implemented logic from dispatchLoopShutdown. This link can be useful http://framework.zend.com/manual/1.12/en/zend.controller.plugins.html#zend.controller.plugins.standard.actionstack

Categories