How to find current controller or action in routes.php in cakephp - cakephp

I try lots of methods to find current controller and action in cakephp routing, but I didn't got answer. Is there any method to find controller and action in routing. If yes, please help me. Or if there is any other way to find these, Please tell me.

It's the routes that decide to what controller and action a url maps to. When routes.php is being loaded the routing process has still not been done (since you are still setting up routes), so you cannot know controller or action is in routes.php.

If the website formatted something like this :
www.johndoe.test/test/test_action/
then probably it can be found on test controller but of course that depends on the settings of the website on how the developer configured it. It may also be found on routes.php like #ADmad said.

Related

how to redirect to home page from Servlet to JSP with Angular UI

I have an AngularJs UI login page and I'm posting the username and password details to a Servlet to validate, and when I'm returning back I'm using sendRedirect from Servlet but it's not working. Here is my sample code
In Servlet:
req.sendRedirect("NewFile.jsp");
It is displaying content inside NewFile.jsp within the login page itself instead of redirecting to NewFile.jsp
I have tried using "$window.location.href" inside AngularJs code but that itself didnt forward to home page.
Any help is much appreciated.
Thank you!
Acording of what you said in the comment of the bounty, I have to say that having everything in only one controller sounds like a bad idea, for more information about this and naming conventions go to Naming conventions and best practices
According to your question, maybe you need to give more details of the problem, it's not clear.
Have you tried to use $location module?
https://docs.angularjs.org/api/ng/service/$location
You can use de .path() module to redirect.
$location.path('#!/');
That will redirect you to the home page.

CakePHP: Can only load index.php, all pages 404 after Cake Bake

I'm trying to bake my first CakePHP application and am unable to get any page to particularly load for me right now. I've updated my config settings for salt,database, etc. and the index.php page tells me that I have configured everything.
So far I've used cake bake all on just one database table so far to make sure it loads properly. I created the Model, Controller, and View for the standard add/index/view/edit pages. When I try to access URL/organizations/index.php I'm hitting a 404 error however.
Is there any troubleshooting someone might have advice for how to solve this one? It is confusing to me that the index.php loads (so it redirects properly when loading the webroot) but trying to view any View pages yields no results. Is there any debug commands I can do to view what the valid pages would be? Or any additional information I can provide?
If you try URL/index.php/organisations or something similar to this and it works, then there is an issue with URL re-writing on the server which you'll need to correct.
I believe if you have things set up correctly you would want to visit /organizations in order to access the index() method of the organizations controller.
In general the ".php" is left off of the URL, as your index.php file initiates all the bootstrapping and routing. This also requires a correct .htaccess setup. Hard to say exactly what the problem is without seeing the app or an error message.
Try in url
URL/organizations/index.php
To
URL/organizations/index
or
URL/organizations/index.ctp
Cakephp using .ctp extension, that means cakephp Template. Please see this link. And also you can see your app\view\Organizations folder. Here all file is with .ctp extension. Isn't it ?

cakephp add new controller path

Background: I'm new to CakePHP and trying to modify an preexisting project.
When I try to add a new UploadsController it is supposed to handle xxxx/uploads requests right?
However, when entering the url neither AppController or UploadsController, allow index.php to do a dispatch. Files are being called. It goes straight to /uploads which is a directory.
I realize this is a bad design to begin with but trying to fix things one step at a time. Need to authenticate before going to /uploads, and than take action.
What am I doing wrong? I tried to modify routes.php to specify controller but that does not work either.
So as I said I am new and fixing someone else's project. I found that there is a security issue where uploads directory is exposed. So I figured I would add Controller to take care of this. uploads folder was under webroot as a result it was going to uploads folder and was not directing to controller. Thanks and hopes this helps someone in the future.. although kind of doubt it since it was a really bad way to do this to begin with.
With the info you gave this is what I can suggest:
Check to see if the UploadsController is in the Controller folder.
Make sure that there's at least a index.ctp file in Views/Uploads/
Check to see if the UploadsController has a default index() method. This is what www.example.com/uploads/ will hit.

Cakephp internal redirect from controller to another controller

Update: i wrote some wrong statements about the use of header in php; so forget that part :)
What i want is to fetch and display a controller's view (with controller's data) from another controller, without have url change in browser.
Some details:
Redirect doesn't do the job because is a direct redirect (via browser)
requestAction doesn't allow me to fetch css and images correctly
I need this thing because i have a controller dispatcher that redirects internally to the other controllers.
I think the only (correct) solution is to use routes.php in /config with Router::connect
and there use the logic that was in the dispatcher controller.
ummm... header() is the function to use for a redirect unless the PHP documentation is wrong. (http://php.net/manual/en/function.header.php) The core in cakePHP uses header for the redirect function (see lines 721 - 730 of cake/libs/controller.php).
So I am not certain what you mean "like normal PHP". CakePHP is PHP, it's just built on object oriented code. It's not magic or twisted ways of doing things. So to do a redirect in cake, you can simply use:
$this->redirect(array('controller' => 'my_controller', 'action' => 'my_action'));
And it will call the header() function.
Now. If you are dead set on not using redirect (maybe if you are going to an external site), you can call header() in the code. Just be sure you put the exit(); after the header call:
header('Location: http://call/my/url');
exit();
It will work just the same as redirect. It's just a lot of unnecessary extra work. Keep in mind that using redirect will maintain the domain name and build the URL for you automatically.
In general, connecting URLs to controllers is the job of routes. If your logic is rather complex and normal routes won't cut it, you can even write your own route parser class that does more complex logic (that's all in the manual).
If this routing logic involves database queries or any other sort of controller logic and may lead to very different output for the same URL based on some internal state though, you're making a very RESTless application and I'd submit you should rethink what you're trying to do. Having said that, you can render any view from any controller action using $this->render(). The controller logic for each view could be put in the AppController or possibly (partly) the models to be called from anywhere. So instead of "redirecting" to a different controller, a route just routes to a specific controller action as usual, that action dynamically calls code it needs to call and then renders the view it needs to render.
If you want your app to stay on the same URL but display very different content, you should probably also look into making an AJAX app.
The right solution for you is probably somewhere in between.

What are the defaults(homepage,controller,etc..) for cakePhp?

I understood (more or less) the separation between the MVC parts in cakePhp, however i cannot understand what are the defaults.
meaning:
What should i edit in order to change the root-entry-point of my site(the known "index.html" or "index.php" file, that shouldn't be changed in cake)?
What controller? What model? What view? What layout?
(hope I'm understood)
(i am using version 1.3)
thanks
What you should edit to modify the root is:
app\views\pages\home.ctp
The default layout can be tweaked here:
app\views\layouts\default.ctp
From there on you can create your menus, links etc to other controllers of other pages, then involving the traditional MVC patterns/conventions you already know.
Addition:
If you want to provide a link to your statistics then use for example:
echo $html->link('My nice statistics',
array('controller' => 'statistics', 'action' => 'show'));
If you would like to embed the statistics then I would use elements: http://book.cakephp.org/view/1081/Elements.
I am not sure about what you missed: maybe the fact you can specify the controller to use for links if it is an external controller to the MVC scope currently used.
Are you sure you have understood the conventions behind MVC? Here is the tutorial I started with some time ago. It is well made but a bit out of date for cakephp 1.3. Nevertheless it illustrates the basic concepts very nicely: Cook web sites fast using CakePHP (IBM)
I hope this is more helpful then :-)
The default Route in Cake routes the address / to the PagesController::display action with the parameter "home", which will make the Pages controller display the file /views/pages/home.ctp. If you just want a static home page, just edit that file.
If your default homepage at / should display more complex data, including model data, you would rather create your own controller with a model and its own directory in /views/ and change the default route for / in /app/config/routes.php to point to an action of that controller.

Resources