Symfony2 generate controller inside custom folder - php

Symfony2 has a command for generating controllers
http://symfony.com/doc/current/bundles/SensioGeneratorBundle/commands/generate_controller.html
Command's default behavior is to generate the given controller inside controller folder within the bundle.
Is it possible to customize the folder where the controller will be generated ( controller/backend for example ) ?

You can try: "php bin/console make:controller Directory\\ControllerName"

You can get all the available options of this command with the help command:
php app/console help generate:controller
No you can't with the current task, but you could extend the GenerateControllerCommand to add custom options. Check out its generate function:
// GenerateControllerCommand.php
public function generate(BundleInterface $bundle, $controller, $routeFormat, $templateFormat, array $actions = array())
{
...
$controllerFile = $dir.'/Controller/'.$controller.'Controller.php';
...
$this->renderFile('controller/Controller.php.twig', $controllerFile, $parameters);
...
}

Related

how to set cron job if using codeigniter in cpanel?

I have a controller called Buy and it has a live method. How to install a cronjob if using codeigniter? I use codeigniter 4,and l use cpanel.
php - q /home/kuslon/kuslon/app/Controllers/Services/Buy.php
STEPS
Create a command-line route in app/Config/Routes.php. I.e:
$routes->setDefaultNamespace('App\Controllers');
$routes->setAutoRoute(false);
// ...
$routes->cli("cli/ship-product/(:segment)", "Services\Buy::ship/$1");
// ...
Where:
cli/ship-product/(:segment) - Represents your route. The (:segment) is optional depending on if your Controller method requires an argument or not.
Buy - Represents your Controller.
ship - Represents your Controller method.
$1 - Represents the optional (:segment) to be forwarded to the first Controller method's argument if in case it requires one. You may omit it if your Controller method doesn't require any arguments.
Notice the use of CLI-only routing with the help of ->cli(...).
Run your cron job.
/usr/local/bin/php -q /home/kuslon/kuslon/public/index.php cli ship-product "ed053cb1-29a4-42f2-a17e-3109fa4d80fe"
Where:
-q - Represents quiet-mode. Suppress HTTP header output.
/home/kuslon/kuslon/public/index.php - Represents the absolute path to your project's index.php file. This assumes that the first kuslon in the path represents your Cpanel username and the second kuslon represents your project root folder.
cli - Represents the first section of the command-line route you defined earlier.
ship-product - Represents the second section of the command-line route.
"ed053cb1-29a4-42f2-a17e-3109fa4d80fe" - Represents the third optional section of the command-line route that may be required by your Controller method.
Sample Controller.
<?php
namespace App\Controllers\Services;
use App\Controllers\BaseController;
class Buy extends BaseController
{
public function ship(string $uuid)
{
// echo "{$uuid}";
}
}
Resource: Running via the Command Line
Addendum:
If in case all your routes pass through an Authentication filter, you may want to exclude these command-line based routes in app/Config/Filters.php. I.e: As you may notice below, I've excluded all routes starting with cli/* from the authfilter.
<?php
namespace Config;
use App\Filters\AuthenticationFilter;
use CodeIgniter\Config\BaseConfig;
class Filters extends BaseConfig
{
// ...
public $aliases = [
//...
'authfilter' => AuthenticationFilter::class
];
public $globals = [
'before' => [
//...
'authfilter' => [
'except' => ['cli/*']
]
],
// ...
];
}
the Main thing You must do is check if request is from command Line and not from A browser in your controller using this is_cli() function .
then you can set the cron job in Cpanel .
good luck

How to put console command from a frontend button?

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().

How does i Get Base Directory URL in zend framework 3?

I am noobie at ZF3, We have placed zend based admin panel inside codeigniter based main app. like following way
my_app/zend_admin/
|
|
--- config
--- module
--- public
i can access zend module using www.my_app.com/zend_admin/my_zend_controller/my_zend_action.
I want to access www.my_app.com/my_ci_controller/my_ci_action.
Is there any method zend provide as ci provides base_url() so i can fetch my ci controller??
to get base URL you can use serverUrl view helper (like in codeigniter base_url())
$this->serverUrl(); // return http://web.com OR
$this->serverUrl('/uri'); // return http://web.com/uri
I am not sure about your setup but try that...
There are several ways you can get this job done using ZF micro tools.
There are some similar view helpers in the ZF like CodeIgniter has. You can use them for the purpose in the view script and layout template.
Lets start up using module.config.php of your module. You can set up base_path key under view_manager key as follows
'view_manager' => [
'base_path' => 'http://www.yoursite.com/',
]
Now if you use the following view helper
echo $this->basePath();
// Outputs http://www.yoursite.com/
If you use the following one
echo $this->basePath('css/style.css');
// Outputs http://www.yoursite.com/css/style.css
But if you do not use the above configuration
echo $this->basePath('css/style.css');
// Outputs css/style.css
As #tasmaniski said about $this->serverUrl(); you can use that too in the view script. Good thing for this does not need any configuration like $this->basePath()
What if you need this in the controller action of ZF. The easiest way to do it in the controller action is
public function indexAction()
{
$uri = $this->getRequest()->getUri();
$baseUrl = sprintf('%s://%s/', $uri->getScheme(), $uri->getHost());
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/
}
Otherwise, you can get it the following way but this works same as $this->basePath()
public function indexAction()
{
// This is for zf2
$renderer = $this->getServiceLocator->get('Zend\View\Renderer\RendererInterface');
// This is for zf3
// Assuming $this->serviceManager is an instance of ServiceManager
$renderer = $this->serviceManager->get('Zend\View\Renderer\RendererInterface');
$baseUrl = $renderer->basePath('/uri');
// Use this $baseUrl for your needs
// Outputs http://www.yoursite.com/uri
}
Moreover, there are two more functions that can be used under different conditions in the controller actions. Those return empty string if rewriting rules used. Those are
$this->getRequest()->getBaseUrl();
$this->getRequest()->getBasePath();
These do not work as you expect I mean as you expect. Must refer to the issue to know why is this!

How to regenerate routes cache when the configuration changes in Symfony 2

I'm currently writing a custom route loader in Symfony 2 that will generate routes based on some configuration options defined in the main config file. The problem is that Symfony caches routes generated by custom routes loaders. Is there a way for me to update the cache when that config file changes?
I defined a configuration like this in app/config/config.yml
admin:
entities:
- BlogBundle\Entity\Post
- BlogBundle\Entity\Comment
My route loader read the config file and generates some routes based on the entities. Now the problem is that once those routes are generated and cached by Symfony I can't change them unless I manually call php app/console cache:clear. What I mean is if I add an entity to the config:
admin:
entities:
- BlogBundle\Entity\Post
- BlogBundle\Entity\Comment
- TrainingBundle\Entity\Training
I will have to manually clear the cache again with php app/console cache:clear in order to create and cache the new routes. I want the routes cache to be invalidated if I change the config, so that a new request to the server will force the regeneration of the routes.
Option 1
If your custom loader class can gain access to the kernel or the container (via DI), you could call the console cache clear command from that class.
E.g.
namespace AppBundle\MyLoader;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\HttpFoundation\Response;
class MyLoader
{
private $kernel;
public function __construct($kernel)
{
$this->kernel = $kernel;
}
public function myFunction()
{
$application = new Application($this->kernel);
$application->setAutoExit(false);
$input = new ArrayInput(array(
'command' => 'cache:clear',
'--env' => 'prod',
));
// You can use NullOutput() if you don't need the output
$output = new BufferedOutput();
$application->run($input, $output);
// return the output, don't use if you used NullOutput()
$content = $output->fetch();
// return new Response(""), if you used NullOutput()
return new Response($content);
}
}
Ref: Call a Command from a Controller
Disclaimer before someone points it out; Injection the kernel/conatiner is not considered "best pratice", but can be a solution.
Option 2
You could also write you own console command that extends Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand that just calls the clear cache command.
Ref ; Call Command from Another
Option 3
This answer also gives you another option

Scaffolding with Laravel

I am relativley new to laravel, and have created an 'article' scaffold using this plugin:
https://github.com/JeffreyWay/Laravel-4-Generators
And running:
php artisan generate:resource article --fields="title:string, body:text"
Everything works out fine, with the table being created in my database and the associated files appearing in my project directory. However, when I navigate to localhost/laravel/public/articles (my directory), I get the following error:
ErrorException (E_NOTICE)
HELP
Undefined offset: 1
Open: C:\xampp\htdocs\laravel\vendor\laravel\framework\src\Illuminate\Routing\Router.php
$route = $this->current();
$request = $this->getCurrentRequest();
// Now we can split the controller and method out of the action string so that we
// can call them appropriately on the class. This controller and method are in
// in the Class#method format and we need to explode them out then use them.
list($class, $method) = explode('#', $controller);
return $d->dispatch($route, $request, $class, $method);
I tried running
php artisan optimize --force
but this didn't help.
Any advice?
you must add route for article like this:
Route::resource('articles', 'ArticlesController');
in app/routes.php file.

Categories