Setting up multiple Kernels in Symfony 4 - php

I am trying to set up a multi-site architecture in Symfony 4 using multiple Kernels.
It would be too lengthy to post all of the changes I have made but I basically followed the Symfony docs for creating a new Kernel and the changes I made can be viewed in the following pull request.
When I attempt to run the api kernel locally (php bin/api server:run) I get the following error message:
I am simply trying to load the home controller and template using the new Kernel
# config/api/routes.yaml
home:
path: /
controller: App\Controller\Home::index

Place routes.yaml under config/routes directory, otherwise Symfony treats this file as a framework configuration file.
Or you can reconfigure kernel to load routes files from api directory by editig configureContainer and configureRoutes methods of ApiKernel.

Related

How to add Request for PHP to Yii2 Project

i want to use Requests for PHP in my yii project.
I installed the yii2 basic template, and copied the Requests Class File (and the Requests directory) to root/vendor.
I include the requests.php file with this line
include('../vendor/Requests.php');
But i always get an error that yii cant find the requests.php class.
What i need to do to implement the requests class?
Use Composer, as proposed in project README. Run this command in root of your project:
composer require rmccue/requests

Yii2 Console: app\models Class not found but common\models found

I have an Yii2 web application with 50+ model class files that are located in /models directory.
Now I want to run some console scripts from /console/controllers/MyController.php using these models but get class app\models\ModelName not found error, despite of use app\models\ModelName at the top.
If I copy a model file to /console/models/ModelName.php or /common/models/ModelName.php (and make change in use) it works alright. Is there any option to use models from /model or should I refactor the application so that both web and console use model files from /common/models
If you are using yii2-advanced build take in mind that #app alias is set each time depending on what part of application you are using.
If you are making a call from frontend, #app will be equal to /path/to/project-root/frontend.
If from backend - /path/to/project-root/backend.
console - /path/to/project-root/console
You may add custom alias in /common/config/bootstrap.php, to make your classes available from root.
For example try to add Yii::setAlias('#root', dirname(dirname(__DIR__))); to /common/config/bootstrap.php and set namespace to root/models
Note: if you will try add #app to bootstrap.php, it will be automatically reassigned by framework.
Note 2: You may check how yii2 autoloader works in BaseYii.php

Fallback route in Symfony 4 while using annotation routing

I'm currently using routing via controller annotations in a Symfony 4 application.
I am trying to route all requests that don't match an existing annotation (e.g. http://example.com/route-that-isnt-defined) to a specific controller and function (e.g. DefaultController::dynamicPage() which has logic to find if I should be serving content or triggering a NotFoundHttpException).
Defining the route for DefaultController::dynamicPage() as #Route("/{param}") precedes and intercepts all other defined routes, making them inaccesible.
I have tried this solution for Symfony 3, not knowing if it will work but get stuck on what "AppBundle" is supposed to refer to, as it's not something that exists in my project.
Currently, my routes.yaml only has one route in it for the index, as all other named routes are defined via annotations:
index:
path: /
controller: App\Controller\DefaultController::index
I am looking for either the proper way to implement the link Symfony 3 solution in Symfony 4, or an alternative method of achieving the routing I want without doing something convoluted like extending the exceptions controller and inserting routing functionality into cases of NotFoundHttpException.
You could try adding a kernel event listener that would handle the kernel.exception event, and for cases where the exception is a NotFoundHttpException you'd return your custom response instead of the 404 Not Found page.
This could be quite flexible since you can implement any custom logic in such listener.
I haven't moved to sf4 yet, but isn't this problem just related to the order of the routes being evaluated? I.e. you could try by just adding explicit definition to load the DefaultController.php annotations in your routes.yml as the last element? I.e. something like this should do the trick (works in sf2.8 at least):
app_annotations:
resource: '#MyBundle/Controller/'
type: annotation
fallback_annotations:
resource: '#MyBundle/Controller/DefaultController.php'
type: annotation
or if that doesn't work in sf4 (if it loads the controller route annotations with some other logic automatically), another workaround would be to just name this fallback controller so that it will be the last one alphabetically (and thus the routes there should be evaluated the last).
From your comment I "smell" you have some composer packages that are not compatible with the current config of your project. Are you upgrading to SF4 from SF3?
I also had this InvalidArguementException:
"The "App" (from the _controller value "App:Default:index") does not exist
or is not enabled in your kernel...
Turns out that I have a non supported package easycorp/easyadmin-bundle version ^3.1 which I fixed it by
Removing the vendor folder
Removing the composer.lock file.
Explicitly specify the version my project supports ^2.3 in composer.json
Run composer install... et Voila!

require_once() Failed Opening Resource in Laravel Valet-hosted project

I am just picking up using Laravel, but I dont like Vue and have been working with the React ecosystem and would like to use React instead of Vue. Laravel Mix doesnt give me the setup I want and so I figured I could use create-react-app.
Using Laravel Valet, I have started a project in which I have also installed create-react-app in a folder called ui, at the root of the Laravel installation.
My idea is to forego some of Laravel's functionality, namely the whole frontend.
I am attempting to require the react app build html file in resources/views/main.blade.php like so:
require_once __DIR__.'/ui/build/index.html';
This gives me the error:
Symfony \ Component \ Debug \ Exception \ FatalErrorException
(E_UNKNOWN) Illuminate\View\Engines\PhpEngine::main(): Failed opening
required
'/Users/Username/Sites/sitename/storage/framework/views/ui/build/index.html'
(include_path='.:')
This path is not correct, but I'm not sure why it is inserting /storage/framework/ into that path.
I have also tried the following, each with a similar error of Failed opening resource:
require_once('../../ui/build/index.html');
require_once($_SERVER['DOCUMENT_ROOT'].'/ui/build/index.html');
require_once(dirname(__FILE__).'/ui/build/index.html');
Laravel does not integrate with Vue, and Laravel Mix is a simple layer on top of Webpack. The default Laravel application ships with Vue scaffolding but removing that is as simple as deleting the files in resources/assets/js and if you wish to use another Javascript library then you can add that into your app.js instead.
The error you're receiving is because Laravel caches view files, meaning that they're served from the cache directory (which lives in storage/framework) so references like __DIR__ are referencing the cache directory, not the resources directory. You can see this in the documentation:
You should avoid using the DIR and FILE constants in your Blade views, since they will refer to the location of the cached, compiled view.
The correct approach to include files into your views with Blade is using the #include directive, e.g:
#include('ui.build.index')
Also, worth noting, that any time you do need to obtain the path to a file in your Laravel application you should use the base_path and app_path helpers.
Prior to continuing with development of your application you should read through the JavaScript & CSS Scaffolding documentation and the Blade documentation, as they contain a lot of information that will be very useful to you — for example, it explains how to replace Vue with React using a single command.

Laravel 5.2 and jenssegers/mongodb REST API with virtualhost

I am creating a REST API with Laravel 5.2 and I'm also using MongoDB with the jenssegers/mongodb library. I have a virtualhost for my application so I can go to the url eg: http://myapp.local.com and simulate how it would be on the real server and as I have some other Laravel projects on dev right now, it is more organised to have them this way.
The virtualhost is pointing to the public folder of my application and it's working correctly.
This project is just starting so everything is being built right now. In order to check the database I created a very simple PostsController just to see if the connection and the results were returned correctly. I added Route::resource('/posts', 'PostsController'); in my routes.php so I could start doing some tests of basic CRUD operations.
When I use php artisan serve and access this route with http://localhost:8000/posts the results are brought correctly but when I try to access via http://myapp.local.com/posts I get this error:
FatalErrorException in Client.php line 56:
Class 'MongoDB\Driver\Manager' not found
Any ideas why this is happening?

Categories