CakePHP Routing new directory - php

I am setting up my cakephp project on a local test environment from my GIT repo.
it is accessed like so:
localhost/projectName/controller/action
However, i get an error saying the controller "projectName" cannot be found.
So, i need it to use localhost/projectName as my "root" directory, and i am kinda lost as to how. I can obviously define a manual route like:
$routes->connect('/projectName/controller/action', ['controller' => 'Pages', 'action' => 'home']);
but i don't know how i can make it a "catch all" instead of just routing to one specific controller / action?
I hope my question makes sense.
thanks

Iy you have created a localhost with xamp or wamp (and create a folder in www with the name of your project) you just need to write :
$routes->setRouteClass(DashedRoute::class);
$routes->scope('/', function (RouteBuilder $builder) {
$builder->connect('/', ['controller' => 'Pages', 'action' => 'home']);
$builder->connect('/{action}', ['controller' => 'Pages', 'action' => '{action}']);
Hope i understand your question and i can help you !
AO

Related

How do explicit routes fail to find the Controller

I set up the following routes. The routes are explicit and the controller is in src/Controller directory. I did a git pull on the stage server and suddenly cakePHP (3.6) could not find the method, and was looking in the wrong controller. Below are the routes that are explicitly coded.
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/<foo>/methodOne/*', ['controller' => 'SomeController', 'action' => 'methodOne']);
$routes->connect('/<foo>/methodTwo/*', ['controller' => 'SomeController', 'action' => 'methodTwo']);
$routes->fallbacks('DashedRoute');
}
This happens with new controllers I create sometimes. I have added the correct namespaces and use statements. To fix this issue I make changes in the controller and then it works. Any idea why this happens? Anyway to prevent this from happening over and over again?

cakephp routing views within sub directory

How do I connect to a view in a subfolder in cakephp 1.3?
If I organize folders in my Views like:
Views/my_folder/my_subfolder/mypage.ctp
How do I connect to the mypage.ctp view in routes? I've tried the following:
Router::connect('/my_folder/my_subfolder/mypage',
array('controller' => 'my_folder', 'action' => 'mypage'));
The above does not work. Is there a different way to write the connection in routes.php?
I want the url to be: www.mysite.com/my_folder/my_subfolder
the Pages controller looks for views in Views/Pages
so move your mypage.ctp there.
The structure of Views folder is guided by conventions, mapping from url to controller+action is the only thing that you change via routes.
Please check it
Router::connect( '/foldername', array('controller' => 'controllername', 'action' => 'functionname'));

PHP Kohana 3.3 Controller Sub Folder

I've looked at so many Stackoverflow questions regarding this and none of them seem to solve my problem. I just want to have a admin folder and controllers within those. Here is what my route looks so far
/*
* Set the routes. Each route must have a minimum of a name, a URI and a set of
* defaults for the URI.
*/
Route::set( 'default', '(<controller>(/<action>(/<id>)))' )
->defaults( array(
'controller' => 'dashboard',
'action' => 'index',
) );
Route::set('admin','admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'admin',
'controller' => 'dashboard',
'action' => 'index',
));
As Kingkero said in his comment, move the route above the default one and it will work. if you read the docs on routing properly (I know it takes a while and a few reads for it all to sink in if you're new to it all, I've been there myself) it should be clear that the default route is a catch-all, and that any specific routes you need should come first, and any catch-all type routes after, as they are tried in sequence and when a match is found no more routes are tried.

CakePHP - How to link to to a file that I created and doesn't come with the package

I am trying to make changes on a CakePHP cms site. I found "main_menu.cpt" file where is the main menu located. Following the existing menus I added my own:
<li>
<a href="<?=$html->url('/'.$lang.'/orders');?>"<?=($page=='orders')?'class="active" ':''?>>
<?__('orders')?>
</a>
</li>
which is pointing to file orders.php but when I click on the link I get this message:
Not Found
Error: The requested address '/en/orders' was not found on this server.
Where I must upload the file orders.php?
I know it sounds stupid, but this cms is totally new for me and even the directory structure doesn't help me :) Hope you will do it !
Thanks in advance
edit ################
<?php
Router::connect('/', array('controller' => 'dpages', 'action' => 'home', 'lang'=>'bg'));
Router::connect('/:lang/', array('controller' => 'dpages', 'action' => 'home'), array( 'lang' => 'bg|en'));
Router::connect('/:lang/pages/:action/*', array('controller' => 'dpages'), array( 'lang' => 'bg|en'));
Router::connect('/:lang/:controller/:action/*', array('action' => 'index'), array( 'lang' => 'bg|en'));
Router::connect('/pages/:action/*', array('controller' => 'dpages'));
Router::connect('/dpages/*', array('controller' => 'dpages', 'action' => 'view'));
Router::connect('/admin', array('admin'=>1, 'controller' => 'dpages', 'action' => 'home'));
Router::connect('/tests', array('controller' => 'tests', 'action' => 'index'));
?>
CakePHP is a framework.
You may need an OrdersController that has a view file for each method.
Check to see how the other pages are build (what files you have in the Controllers folder).
If you just want to add a content page, for orders, you can put it inside the View/Pages folder, and call it orders.ctp.
You can access it on site.com/pages/orders
Add the following line in Config/routes.php (among the other Router::connect lines)
Router::connect('/:lang/orders', array('controller' => 'dpages', 'action' => 'orders'), array( 'lang' => 'bg|en'));
Go to Controller/DPagesController.php and add:
public function orders() {
// can be blank for now
}
Go to views/dpages folder and create orders.ctp and put the static form in there.
Then try the link again.
CakePHP is not designed for you to add additional php files like that, nor should it be. Therefore I highly recommend that you rewrite whatever you have in your orders.php file to be use the CakePHP framework.
However, you can add orders.php into your app/Vendor folder, then include it in a controller's action (example: include_once(APP . 'Vendor/orders.php'); ). Then, you should be able to access it via http://yourwebsite.com/controller/action where controller is the name of the controller you chose to put the include in, and action is the name of the action you chose to put the include in.

Kohana 3.3: How do I create routes for an admin sub-directory?

I have set up two routes, one is the default and the other is one to enable the admin section which has controllers in a sub-directory of the controller directory. These are how they look like:
Route::set('default', '(<controller>(/<action>(/<id>)))')
->defaults(array(
'controller' => 'Home',
'action' => 'index',
));
// Admin routes
Route::set('admin', 'admin(/<controller>(/<action>(/<id>)))')
->defaults(array(
'directory' => 'Admin',
'controller' => 'Main',
'action' => 'index',
));
When I navigate to /admin/ or /admin/main I get a 404 error and I can't get it to work. I've also named the classes in the admin sub-directory as Controller_Admin_Main so that should work, right?
Please provide examples to how this should be done correctly. Thank you very much! :)
The problem got fixed by switching the position of the admin route to above the default route in the code. I guess kohana matched the first expression and tried to show a view according to the default route.

Categories