Using plain old apache server, you make your request to Symfony using localhost:80/acme/app.php/routes
So app.php or app_dev.php is you Front Controller. Knowing this, you can forget almost it. But when you use the PHP built in server, you can access directly to localhost:8000/routes.
I have looked inside the console script file, and it looks like app.php but we call it only once, at the start of the server. Where is the glue stuff ?
The console file is the front-controller for the CLI environment of your Symfony application. The server:run is part of this environment and can be found in the FrameworkBundle: ServerRunCommand (EDIT: as of Symfony 3.3, the command can be found in the WebServerBundle)
It starts the built-in webserver of PHP: php -S localhost:8000 and it routes all incomming requests to a so-called routing script. In case of the dev environment, the router_dev.php router inside the FrameworkBundle.
This router file has this line:
$_SERVER['SCRIPT_FILENAME'] = $_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'app_dev.php';
Which pretends that the incomming request was made to the app_dev.php file. (So localhost:8000/something becomes localhost:8000/app_dev.php/something after this router script). It then includes the app_dev.php file to handle the rendering of the site.
Related
I got my Symfony 3.4 application deployed using PROD environment following this guide: https://symfony.com/doc/3.4/deployment.html (seems that, by default, was running on PROD, since I does not selected any environment during installation...)
In the near future, this machine will take the PRE-PRODUCTION role, so I created a new environment called pre for my application following this guide: http://symfony.com/doc/3.4/configuration/environments.html#creating-a-new-environment
Now I'm wondering how to switch this machine to new PRE environment.
I read these guides, but I'm still confused:
1) http://symfony.com/doc/3.4/configuration/environments.html#executing-an-application-in-different-environments
2) http://symfony.com/doc/3.4/setup/web_server_configuration.html
On the current machine, I'm using Apache; but for production, and following updates, I'll considere to start using NGINX. So, both options are appreciated.
If you've followed the instructions in the documentation you've entered:
Because you'll want this environment to be accessible via a browser, you should also create a front controller for it. Copy the web/app.php file to web/app_benchmark.php and edit the environment to be benchmark
then you have app_pre.php front controller with this line:
$kernel = new AppKernel('pre', false);
Just point your Apache web server to use app_pre.php instead of app.php as the front controller and your environment is switched.
My app/config/app.php has
'url' => 'http://dev.domain.com/something/somethingElse'
Then I have a function that can be called from the application and also from an artisan command. But URL::route('myRoute') returns different results. When called from the application it returns http://dev.domain.com/something/somethingElse/myRoute, but in the artisan command http://dev.domain.com/myRoute.
URL::to has same behaviour.
Note: I do not have any other app.php file defined for other environments that could overwrite the global one.
Any ideas why this happens ?
Thanks!
A Laravel-generated URL consists of a number of parts:
Scheme: http://, https://, etc.
Host: dev.domain.com, localhost, etc.
Base: something/somethingElse (the subdirectory on the web server)
Tail: myRoute, (the Laravel route parameters)
These parts are then concatenated to form the URL.
Ultimately, Laravel generates the URL using the $_SERVER request variables. The function prepareBaseUrl() in Symfony\Component\HttpFoundation\Request is what is ultimately used to determine the base part of the URL.
When you make a request through your web browser, the request goes to ~/public/index.php and the necessary $_SERVER variables are populated with the correct information for Laravel to populate the base part of the URL.
However, when you make the request using Artisan on the command line, the request goes to the ~/artisan script. This means that the $_SERVER variables are not populated in the same way and Laravel is not able to determine the base part of the URL; instead, it returns an empty string ''.
From what I can find, it doesn't look like there is any appetite from the Laravel team to enable the application to function out-of-the-box in a subdirectory, e.g. Bugfix: domain routing for subfolder.
I ended up working around it in the way described by #medowlock for my scripts that would be called from the command line, e.g.:
Config::get('app.url') . URL::route('route.name', ['parameter'=>'value'], false)
This concatenates the application URL set in the app/config/app.php and the relative URL route.
If you use Laravel 4.2, you can call the URL::forceRootUrl function to explicitly define what the root url of your laravel application is.
URL::forceRootUrl( Config::get('app.url') );
Calls to URL::to will use the URL define in your app config file after forcing the Root URL.
Unfortunately, this isn't available in Laravel 4.1 or Laravel 4.0
I'm building a cron-like feature into my Laravel application. To ensure only 1 instance is running across multiple startup requests, I'm making the cron call to a Shell script which finally executes a PHP file.
In this PHP file in need of course access to Laravel's built-in functions and Facades. Currently I'm requiring the public/index.php file to bootstrap the Laravel environment. Everything is working fine, I'm able to call interact with the Laravel environment. However, it's also producing an error on each startup:
local.ERROR: exception 'Symfony\Component\HttpKernel\Exception\NotFoundHttpException' in
/PATH_TO_LARAVEL/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:140
How can I prevent this error from being thrown? Or is there another way to bootstrap into Laravel?
I've also tried to require the bootstrap/autoload.phpand bootstrap/start.php files separately but then the environment doesn't seem to be bootstrapped.
Thanks.
The trick is that you need to require both the ./bootstrap/autoload.php and ./bootstrap/start.php scripts for the Laravel environment to start up.
Try making a file: ./bootstrap/cronloader.php:
<?php
// Laravel Autoloader
require __DIR__.'/autoload.php';
// Start Laravel
require_once __DIR__.'/start.php';
Then simply require_once this at the start of your script.
Alternatively, write an artisan command to replace your script? Since you can call artisan directly from cron, and it will give you the full laravel environment.
I'm trying to work with Symfony2 with the new PHP 5.4 and its built-in server.
I downloaded Symfony2 and unziped it on my server and added this router.php file like mentioned here:
<?php
if (isset($_SERVER['SCRIPT_FILENAME'])) {
return false;
} else {
require 'Symfony/web/app.php';
}
?>
The webserver itself works because if I replace router.php with something simple like phpinfo(); it outputs it correct but with the mentioned router.php script the site remains white/blank. If I open developer tools it returns 500 Server error.
I start the server like that:
/home/php54/php -S 0.0.0.0:88 router.php
On my shell I have no output of a error message.
/home/php54/php -S 0.0.0.0:88 router.php
You are trying to run server on privileged port, so either change port or run this as privileged user (not recommended).
Also you have modified router script and I think you've messed up with file paths. You are not specifying docroot in your command, so your current directory is your docroot (so it should be your project's web/ directory). But then path to the front controller in router.php is wrong (Symfony/web/app.php).
You should really follow carefully instructions from my blog post. So:
change your current directory to your project's web/ directory,
download router script: wget https://raw.github.com/gist/1507820/b9583ab7f7f5e0e4e29806c38c6c361220b6468f/router.php,
run server: php -S 0.0.0.0:8000 router.php.
This should work.
You can also try patch from my pull request which adds simple command that just runs built-in PHP server.
I don't have a php 5.4 environment handy, but load app_dev.php instead of app.php, as debugging will be set to true and errors will be reported.
I have a php code where I uses sql connections and creating some results. But i don't use any symphony related codes. Where should I deploy those kids of data. At the moment It gives me following errors. But When i run the same code in my localhost WAMP server it works fine.
404 | Not Found | sfError404Exception
Empty module and/or action after parsing the URL "/race/getitemnames.php?eventid=1" (/).
stack trace
at ()
in SF_ROOT_DIR/lib/vendor/symfony/lib/controller/sfFrontWebController.class.php line 44 ...
if (empty($moduleName) || empty($actionName))
{
throw new sfError404Exception(sprintf('Empty module and/or action after parsing the URL "%s" (%s/%s).', $request->getPathInfo(), $moduleName, $actionName));
}
// make the first request
at sfFrontWebController->dispatch()
in SF_ROOT_DIR/lib/vendor/symfony/lib/util/sfContext.class.php line 170 ...
at sfContext->dispatch()
in SF_ROOT_DIR/web/index.php line 7 ...
Please check your RewriteRule.
If you want to do that you'll have to place the external script somewhere beneath your web root, which I assume will be SYMFONY_PROJECT_DIR/web/race/getitemnames.php.
Ideally, you should not put these kind of scripts inside a symfony project. They may open up your project for security risks and other unforeseen behaviours.
I'd suggest to only have your symfony project run at that particular vhost, and if you need to run another framework/script/whatever create a separate virtual host for it with a different web root. For example have your symfony project run from www.mydomain.com, and create a separate host scripts.mydomain.com which will serve stand-alone scripts.
If it is a simple script, you should also investigate if you could rewrite it to run from a symfony action. If possible, create a new module and/or action in your symfony project that executes your script's code. Add a route to it, and you're done.
Also check wether you have set
<directory "path/to/project">
Allowoverride All
</directory>
for your vhost on apache.