Run Silex Application in Command Line - php

I'd like to run a Silex Application like this in Command Line:
$app = new Silex\Application();
$app->get('/hello/{name}', function($name) use($app) {
return 'Hello '.$app->escape($name);
});
$app->run();
I think for that purpose, i'd have to pass Symfony's Request Object as first parameter to the run method, but i've got no idea, where to set the Url-Path to make it work. Any Ideas? Or is there a better way to do this?

Here's a simple way to do it:
list($_, $method, $path) = $argv;
$request = Request::create($path, $method);
$app->run($request);
And then on the command line:
$ php console.php GET /

If you want to use silex in a command line, you need to use the Console Component, here a tutorial for silex: http://beryllium.ca/?p=481
Then you are able to call a twig (symfony) service, and to forward an action !
http://symfony.com/doc/current/cookbook/console/console_command.html#getting-services-from-the-service-container

Related

Slim Php not recognizing gets

I am just starting to work with Slim PHP. What could be the reason that get is not recognized on server?
This route works: it returns required text https://mywebsite/back/public
This route doesn't work (Not Found): https://mywebsite/back/public/countries
I have just installed slim framework and added new index.php file.
<?php
require '../vendor/autoload.php';
$app = new \Slim\App();
$app->get('/', function($request, $response, $arg) {
$response->write("This route works");
});
$app->get('/countries', function($request, $response, $arg) {
$response->write("This route doesnt");
});
$app->run();
?>
What version are you using? If you are using version 4, you need to set the basePath.
$app->setBasePath('/back/public');
Also enable mod_rewrite and configure your .htaccess file.
I recommend you check this project on github slimphp/Slim-Skeleton. This is a boilerplate code for Slim 4 applications.

Calling a controller in Laravel 4

I have a controller prefixed with a namespace. How can I execute that controller? Without necessarily make a Request object on the fly. What should I do?
I have found this solution on the net:
$app = app();
$controller = $app->make(ucfirst("MyNamespace")."\\".ucfirst($controller_name)."Controller");
return $controller->callAction($controller, $parameters = array());
I get an error that says `callAction()`'s first parameter must be of type `Container`.
How should I execute a controller in Laravel from inside another controller?

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.

Laravel 5 on shared hosting - wrong public_path()

I deployed a Laravel 5 project to a shared hosting account, placing the app-files outside of the www-folder, placing only the public folder inside of the www-folder. All like explained here, in the best answer: Laravel 4 and the way to deploy app using FTP... without 3. because this file doesn't exist in Laravel 5.
Now when I call public_path() inside of a controller i get something like my-appfiles-outside-of-www/public instead of www/public. Does anyone know why I doesn't get the right path here? Can I change this somewhere?
Thanks!
You can override public_path using container.
Example:
App::bind('path.public', function() {
return base_path().'/public_html';
});
$app->bind('path.public', function() {
return __DIR__;
});
write above code in public_html\index.php above of this code
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
There are a number of solutions, like a change to index.php or the accepted one with the ioc container.
The solution that works best, in my opinion, is described in this SO question:
Laravel 5 change public_path()
Why is it better than other solutions? Because it works for regular http request, use of artisan ánd use of phpunit for testing! E.g. the change to index.php works only for http requests but leads to failures when using PHPunit (in my case: file (...) not defined in asset manifest).
A short recap of that solution:
Step 1: In the file: bootstrap/app.php change the very first declaration of $app variable from:
$app = new Illuminate\Foundation\Application(
realpath(__DIR__.'/../')
);
to:
$app = new App\Application(
realpath(__DIR__.'/../')
);
This points to your own custom Application class, which we will create in step 2.
Step 2: Create the file Application.php in the app folder:
<?php namespace App;
class Application extends \Illuminate\Foundation\Application
{
public function publicPath()
{
return $this->basePath . '/../public_html';
}
}
Be sure to change the path to your needs. The example assumes a directory structure like so:
°°laravel_app
°°°°app
°°°°bootstrap
°°°°config
°°°°...
°°public_html
In case of the OP, the path should probably be
return $this->basePath . '/../www/public';
(up one from laravel_app, then down into www/public)

How to do simple testing of a Laravel controller?

In most web apps I've worked on, I can create a simple test script (test.php), load all dependencies (usually through an autoloader), setup the database connection, make calls to any class methods, then examine the results they return to make sure everything looks right.
Example:
$test = new Item;
var_dump($test->getItemStatus($itemid));
The above would show a nice output of the values it returns for that itemid.
With Laravel it appears to be much more complex to just perform this simple test...or maybe I am overcomplicating it and there is a simple way to do this.
Suppose I want to do the same thing in a Laravel 4 app. I want to test the output of method getItemStatus in controller named ItemsController that uses the model Items.
If I try the following, I get an Symfony \ Component \ Debug \ Exception \ FatalErrorException error:
$items = App::make('ItemsController')->getItemStatus($itemid);
If I define this route in routes.php:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController#getItemStatus'));
Then try the following, I get Symfony\Component\HttpKernel\Exception\NotFoundHttpException in the request output:
$request = Request::create('items/get-item-status', 'GET', array());
$items = Route::dispatch($request)->getContent();
In my opinion in Laravel is much more simple and maybe that's why you might be overthinking it.
Let's start with a very basic router. Here are some options to do simple debug using Laravel applications in a basic router/controller:
Route::get('test', function() {
$test = new Item;
dd($test);
Log::info($test); // will show in your log
var_dump($test);
});
Now follow:
http://yourserver.com/test
And it should stop on
dd($test);
Vardumping and dying at that line. Comment it, try again, and so on.
All of these will also work in a controller:
Route::get('test/{var?}', 'TestController#index');
The very same way:
class TestController extends Controller {
public function index($var = null)
{
$test = new Item;
dd($var);
dd($test);
Log::info($var);
Log::info($test);
var_dump($test);
var_dump($var);
}
}
Your route:
Route::get('items/get-item-status', array('as' => 'getItemStatus', 'uses' => 'ItemsController#getItemStatus'));
Should also work, if you follow:
http://yourserver.com/items/get-item-status
If it doesn't, try to follow
http://yourserver.com/public/index.php/items/get-item-status
Or just
http://yourserver.com/public/items/get-item-status
Because you might have a virtual host or .htaccess configuration problem.

Categories