Y have this /src/AppBundle/Resources/views/tests/index.html.twig which just contains:
{{text}}
And is controlled from /src/AppBundle/Controller/TestsController.php which was working fine with a Route over the function that renders "text". Now i want to have a routes file which I've located at /src/AppBundle/Resources/config/routing.yml. Apparently in older versions this could be done with:
tests_index:
path: /tests/index
defaults: [_controller: AppBundle:Tests:index]
But maybe I'm missing something or the newer Symfony isn't buying it when I try to reach it at http://localhost/project/web/tests/index. The error is:
Unable to find the controller for path "/pruebas/index". The route is
wrongly configured.
The title of your question is rather strange, because you do want to use a controller. Anyway, your config is not right. It should look like this:
tests_index:
path: /tests/index
controller: App\Controller\TestsController::show
In this case show is the name of the function in your controller.
Also I would advise to use routing annotations. It makes life much easier. Read more about it here.
Related
In \Illuminate\Foundation\Auth\VerifiesEmails.php, line 39 fails with $request->route('hash'), but passes with $request->get('hash'). I am not sure if this is a bug, but I see nothing I have done that would somehow break this function specifically right here. I have not modified my VerificationController.php file from the core either.
The $request->route('id') above works, but the passed ID in this route is not a parameter, but directly in the path whereas the hash is affixed as ?hash=myhash.
For reference, here is my URL: http://localhost:8000/email/verify/8edd16a5-ad04-4782-b0fe-33f0f482d080?expires=myexpiryhere&hash=myhashhere&signature=mysignaturehere
Can anyone explain to me how to get this working? Obviously modifying the vendor files is not an option. I posted this in the Laravel issues here, but was directed out with the suggestion that maybe I forgot a route parameter. The URL is generated by the framework, so I've no idea what parameter I could be forgetting.
This is my bad, but thanks to Chin Leung for asking the right question.
My current routes are:
Route::get('email/verify', [VerificationController::class, 'show'])->name('verification.notice');
Route::get('email/verify/{id}', [VerificationController::class, 'verify'])->name('verification.verify');
Route::post('email/resend', [VerificationController::class, 'resend'])->name('verification.resend');
I specify them myself because I do not use the default namespacing in my routes files, I import the controllers directly to make refactoring easier. It looks like a new param was added to the verification.verify route later on, so it should now be email/verify/{id}/{hash}
I'm working on an application that is growing rapidly which is causing more and more controllers over time and I'm always trying to maintain good practices for do not have as many lines per controller. Right now are almost 40 controllers in Controller directory and it's a bit complicated found one when need to add code or edit or something else so I'm thinking in order them inside subfolders under Controller directory as follow:
src\
AppBundle\
Controller\
Comunes\
CiudadController.php
DuplicadosCedulaController.php
...
RegistroUsuarios\
EmpresaController.php
NaturalController.php
...
RPNI\
CodigoArancelarioController.php
RPNIProductoPaso1Controller.php
...
BuscarEmpresaController.php
DistribuidorController.php
...
But that reorder is causing this error on my application:
FileLoaderLoadException: Cannot import resource
"/var/www/html/project.dev/src/AppBundle/Controller/" from
"/var/www/html/projectdev/app/config/routing.yml". (Class
AppBundle\Controller\EmpresaController does not exist)
Since apparently Symfony is not able to find the controller class when it's not on Controller directory. I've found this topic but is not clear to me what the problem is. I don't know if this is possible or not I read Controller Naming Pattern at Symfony docs but is not so helpful. Any advice around this? A workaround? Suggestions for a better project structure organization?
Note: I made only one bundle because has no sense more than one since the application will not works for separate bundles so following Syfmony Best Practices I come with only one bundle
Edit
This is weird and I don't know how all is working again, I've move all the controllers from Controller to subfolders inside that directory as my example above shows and didn't change nothing at all on routing.yml and Symfony keep getting controllers even if they are in subfolders: amazing!! Ahhh very important just remember CLEAR CACHE command, the most important Symfony command I believe, many of developers issues are cause of this, I forgot complete to clear it and test changes!!
Here is a working example:
routing:
st_mainsiteweb_admin_subsite_create_template:
path: /subsite/create-template
defaults:
_controller: STMainSiteWebBundle:Admin/SubSite:createTemplate
directory structure:
ST\
MainSiteWebBundle\
Controller\
Admin\
SubSiteController -> createTemplateAction
Is this are you looking for?
I do it 2h, now is ok, no routing, no other file.
Just change the namespace of
CiudadController.php
DuplicadosCedulaController.php
....
from AppBundle\Controller to AppBundle\Controller\Comunes
I never tried that so it's just a theoretical answer. If you want to have the controllers like this is perfectly fine, but then you should need to map them in the routing.
Controller\
Comunes\
CiudadController.php
DuplicadosCedulaController.php
Then will be matched in routing yaml:
comunes:
resource: "#yourBundle/Controller/Comunes"
type: annotation
And so on for every different directory. As far as I know they are automatically loaded from Controller/ directory, but if you place them on any other place you need to reference them in the routing.
Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}
I heve an embedded controller in my base template. It's a search bar.
For the search bar controller, I have a route "myProject/search".
What I would like is that this route will be taken only when the template where I am embedding the controller (base.html.twig) will call it, and not when i manually put in the browser: "myproject/search".
Any idea on how to do that.
I think, since some time you can't do it:
http://symfony.com/doc/current/book/templating.html#embedding-controllers
quote from the docs:
Even though this controller will only be used internally, you'll need
to create a route that points to the controller
(...)
Since Symfony 2.0.20/2.1.5, the Twig render tag now takes an absolute
url instead of a controller logical path. This fixes an important
security issue (CVE-2012-6431) reported on the official blog. If your
application uses an older version of Symfony or still uses the
previous render tag syntax, you should upgrade as soon as possible.
Anyway, I guess, you can try do it yourself by passing some "secret" argument to search action when you call it from your template. Next in the action you check if the argument was passed to it, and if not you throw 404.
Another way to achieve your goal is use .htaccess file.
You can restrict your route to a certain method by _method option in your routing configuration:
your_rote:
pattern: /myProject/search
defaults: { _controller: YourBundle:YourController:YourAction }
requirements:
_method: POST
Let's say I have this in my bootstrap.php file in a cakePHP application:
Configure::write('Config.language', 'eng');
How can I dynamically change the value of that configuration variable based on user action in a controller of my application? I tried to do the same thing that I did above here in my controller, but that didn't work
Any help?
Try Configure::write('Config.language', 'dut'); for e.g.
This answer from the question suggested by #Ryan Pendleton shows a somewhat correct way to use this directive.
It should be used in the AppController because it gets loaded first - as the parent of all other controllers in the application itself.
I used "somewhat correct" because it is best to validate the language codes ('eng', 'fre', 'dut') in the app/config/routes.php file - go here for more information.
Also do check out this: Internationalization-Localization explanation.