I am new on Cakephp. I am using latest version of cakephp. I have created an controller "PostsController" and want to make it home page. But when I have set it to home page from routes.php nothing happens. I am using subdomain like - cakephp.example.com. Here is my routes.php code
$routes->connect('/', ['controller' => 'Posts', 'action' => 'index', 'home']);
Can anyone please help me why it is not working? Is there anything need to do in htaccess file?
Router: (only once in your router class)
$routes->connect('/', ['controller' => 'Posts', 'action' => 'index']);
src/Controllers/PostsController:
public function index()
{
// your code here
}
src/Template/Posts/index.ctp
<h1>Hello world</h1>
Related
I am new in cakephp3. Can anyone tell me how we use redirection in the cake php 3. My concept is this if user login then it's redirect to the dashboard controller which is in Backend folder.
You're most likely looking for the 'prefix' attribute
Example:
return $this->redirect([
'prefix'=>'backend'
'controller' => 'Dashboards',
'action' => 'index'
]);
https://book.cakephp.org/3.0/en/controllers.html#redirecting-to-other-pages
Trying to play around with a test API. Based on the following routes setup, if I request /v1/tests/index.json I will get a JSON Object Response as expected, but if I request /v1/test/index.json I will get an error that TestController is missing. I have checked docs and I can't seem to figure out what is wrong. I expected the $routes->connect('/test', [...]); to work, but it is not. Any help in shining some light into this is appreciated.
<?php
use Cake\Core\Plugin;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
Router::defaultRouteClass('DashedRoute');
Router::extensions(['json', 'xml']);
Router::scope('/', function (RouteBuilder $routes) {
$routes->prefix('v1', function (RouteBuilder $routes) {
$routes->connect('/test', ['controller' => 'Tests', 'action' => 'index']);
$routes->fallbacks('InflectedRoute');
});
$routes->fallbacks('DashedRoute');
});
Plugin::routes();
There is no explicit route set up matching /v1/test/index.json. Your:
$routes->connect('/test', ['controller' => 'Tests', 'action' => 'index']);
route will match /v1/test or /v1/test.json|xml, and that's all.
/v1/test/index.json will be catched by the fallback routes, and hence try to connect to the controller matching test, ie TestController.
Check out Cookbook > Routing > Connecting Routes more closely, you're doing what is shown in the /government example.
Did you try to specify the action in the route connect?
$routes->connect('/test/index', ['controller' => 'Tests', 'action' => 'index']);
I'm using cakephp 2.3.8 and I want to create custom content for my homepage. I'm having trouble getting anything from the Pages Controller to the home.ctp view
In my Pages Controller I've created a home function :
public function home() {
$test = 'test';
$this->set(compact('test'));
}
But when I go to home and check with the debug kit I can see there is no $test variable.
How do I get information from the controller to the view for the home page?
My guess is that the default route for the pages controller is causing you trouble:
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
So when you go to /pages/home, it thinks it's got to execute the 'display' action still.
Try putting a route above it like this:
Router::connect('/pages/home', array('controller' => 'pages', 'action' => 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
That way, the home page will render your home action, and all others will render the default 'display' action.
That's all in /app/config/routes.php.
I'm creating a website using CakePHP.
It has user Registration and Login system.
So at first i have a controller called HomeController which has three methods
1) index();
2) login();
3) register();
by default index() method will execute.
i have the following code to call other two methods.
<? php
echo $this->Html->link('Login',array('controller'=>'Home','action'=>'login'),array('escape'=>FALSE));
echo $this->Html->link('Register',array('controller'=>'Home','action'=>'register'),array('escape'=>FALSE));
?>
So now when i click on the above links (login,register) it will call appropriate method and the url will be something like
www.example.com/home/login and www.example.com/home/register
Now i want to remove the controller name from the url since the method is in the same controller.
So the url should look like
www.example.com/login and www.example.com/register
is it possible??
Please help..
in app\Config\routes.php add following lines
Router::connect('/login', array('controller' => 'home', 'action' => 'login'));
Router::connect('/register', array('controller' => 'home', 'action' => 'register'));
It will convert default urls to your desired urls
www.example.com/login and www.example.com/register
Router::parseExtensions('json');
Router::connect('/*', array('controller' => 'home',
'action' => 'login'));
Router::connect('/login', array('controller' => 'home',
'action' => 'login'));
I'm developing a simple CMS in CakePHP, right now it has 4 controllers in it(Menus,Site,Roles,Users), I want to rewrite one controller, but I'm having problem.
I use all the actions only as admins for admin purpose like admin_view, admin_add......
except siteController(this controller is only for frontend purpose)
I need my www.example.com/site/view/something_here must be replaced to www.example.com/something_here - this will be displayed in front-end so.
I added a line in my routes file:
Router::connect('/*', array('controller' => 'site', 'action' => 'view'));
But after adding this I couldn't able to use other controllers.
I again added some more lines before the above line:
Router::connect('/admin/Menus/*', array('controller' => 'menus', 'prefix' => 'admin'));
Same for all other controllers, but if I send any action or id in url it doesn't works.
like - http://www.exmple.com/admin/menus/[view/1] - the one inside square bracket doesn't works.
any Ideas on rewriting this?
I just answered a similar question on another thread.
To put the admin controller routes before the '/*'-route was the right idea, but the way you did it the router can't assign an action. You could use the following for each controller:
Router::connect('/admin/Menus/:action/*', array('controller' => 'menus', 'prefix' => 'admin'));
Or you could use the default prefix-routing routes, so you don't have to add a route for each new controller.
// prefix routing default routes with admin prefix
Router::connect("/admin/:controller", array('action' => 'index', 'prefix' => 'admin', 'admin' => true));
Router::connect("/admin/:controller/:action/*", array('prefix' => 'admin', 'admin' => true));