routing in CakePHP 3 - cakephp

I'm working on CakePHP 3.3
I have some dashboard controllers which are named like
DashboardUsersController.php,
DashboardBusinessesController.php,
DashboardCustomersController.php,
etc
I want to map urls like
http://example.com/dashboard/users/view/param1/param2
And this will call DashboardUsersController.php class and view function with param1 and param2 as parameters.
In short I want to change url http://example.com/dashboard-users/view/param to http://example.com/dashboard/users/view/param
this type of mapping will be done only if dashboard is present after domain, otherwise it will work as default like on accessing http://example.com/users/view/param1 will call UsersController.php
What I have done till now?
Since, I'm new to CakePHP and routing, I have no idea where to start from and therefore have done nothing till now. I need your help.

I think what you needed is prefix. Bake your controller model with prefix dashboard .
Use this in you routes.php
use Cake\Routing\Route\DashedRoute;
Router::prefix('dashboard', function ($routes) {
// All routes here will be prefixed with `/dashboard
$routes->fallbacks(DashedRoute::class);
});
And remove that dashboard part from controllers or remove dashboard from your table name and rebake everything with --prefix .
bin/cake bake all --prefix dashboard
These links will help you
https://book.cakephp.org/3.0/en/development/routing.html#prefix-routing
https://book.cakephp.org/3.0/en/bake/usage.html

Related

App::build in CakePHP 3

Firstly, I'm very new in cakephp. In version 2.x, it allowed App::build to specify Controllers in specified folder. For example:
App::build(array(
'Controller' => array(
ROOT . '/app/Controller/Api/'
)
));
But in cakephp3.x, App::build is no more available. So how can I make a same thing in cakephp3.x ?
As written in the cakephp doc here : http://book.cakephp.org/3.0/en/appendices/3-0-migration-guide.html#configuration, the App::build is not a part of cakephp3 anymore.
So, you'll have to make a specific configuration for the cakephp autoloader (use composer):
"autoload": {
"psr-4": {
"App\\Controller\\": "/path/to/directory/with/controller/folders"
}
}
More information about this config : http://book.cakephp.org/3.0/en/development/configuration.html#additional-class-paths
More information about composers autoloader :
https://getcomposer.org/doc/01-basic-usage.md#autoloading
App::build has been removed but what you want can be done with prefix routing in Cake3. This is exactly what you try to solve. Taken from the documentation:
Prefixes are mapped to sub-namespaces in your application’s Controller namespace. By having prefixes as separate controllers you can create smaller and simpler controllers. Behavior that is common to the prefixed and non-prefixed controllers can be encapsulated using inheritance, Components, or traits. Using our users example, accessing the URL /admin/users/edit/5 would call the edit() method of our src/Controller/Admin/UsersController.php passing 5 as the first parameter. The view file used would be src/Template/Admin/Users/edit.ctp
Just replace admin with api from the example and read the whole section of the manual I've linked and you're done.

CakePHP 3 routing resource with different name

I am creating REST API using Cake resource. I have route for users:
/users
and now I want to create nested resource for users on projects
/projects/:projectId/users
However I don't want to use UsersController for this one, I want to use different controller. My routing looks like this:
$routes->resources('Users');
$routes->resources('Projects', function ($routes) {
$routes->resources('Members');
});
I don't know how to set up that the route for MembersControlles is not members but users.
There are no aliases for resources from memory. The string passed to the resource is the controller name. So passing 'Members', CakePHP is going to look for the MembersController. But your Entity is obviously called User and your controller is UsersController? Which should contain your index, add, edit, delete methods for the RESTful API.
To create an alias you could try inheritance, you could create a MembersController and have it extend your UsersController?

cakePHP custom view folder

I have a UsersController and index() action so by default cakePHP will look for the index.ctp in Users view folder. What i would like to do is to create a separate folder called Partials and have the controller look for the view inside the folder instead. Please help as I just started my journey in cake. Thanks
You can define the path to your view by $this->viewPath in your controller action. E.g. for the following folder strucuture:
... you can access the various templates by the following statements:
function index()
{
// by code conventions this action automatically matches /Home/index.ctp
$this->viewPath = "/Partials/"; // matches /Partials/index.ctp
$this->render("mypartialview"); // matches /Partials/mypartialview.ctp
}

How to use hash (#) on urls in chaplin.js like backbone used to do?

let's say that I have two "pages" (endpoints) on a chaplin.js site
the routes:
match('', 'first_controller#show');
match('second_view', 'second_controller#show');
and two links:
Go to home
Go to Second
the generated urls are "correct":
mysite.com/something/ (home)
mysite.com/something/second_view (second view)
(notice that I'm not on the root of the site). When I start the application at "home" and then click the "Go to second" link i get correctly redirected to the second view, everything gets tendered correctly and the url on the browser changes to mysite.com/something/second_view
But then I cannot refresh the navigator since my webserver will try to reach a second_view folder instead, and I'll get a 404.
What i need is to always generate the urls using a # like in backbone, something like mysite.com/something/#/second_view.
BTW: that last link works but chaplin deletes the # (like a redirect)
Maybe I need to configure something? or change something on the ùrl`helper, I couldn't find anything in the docs. Any Ideas??
Thxs
Backbone itself allows this functionality out of the box, through
Backbone.history.start({pushState: false})
(the default)
You can see the startHistory call here.
You just have to pass this options object as a second parameter to initRouter in your Application :
this.initRouter(routes, {pushState: false});

CakePHP Redirect to External URL

In CakePHP, I want to create a custom URL that points from my site to another site.
Example: example.com/google would redirect to http://www.google.com
I'm a self-taught CakePHP newcomer and just can't figure out the steps. From my homework, I think I can create a route to a controller/action in config/routes.php, but I don't the right terminology to create the action in the controller.
If you want to redirect directly form controller to an external url the we can directly use
$this->redirect('http://www.google.com');
from our controller. It will redirect you to the mentioned address. This works perfectly fine.
You don't want a "redirect", you want to create a hyperlink.
Use Cake's built-in Html helper.
In your controller...
var $helpers = array( 'Html' );
In your view...
echo $this->Html->link( 'Google link!', 'http://www.google.com/' );
A "redirect" is commonly used to refer to redirecting the script on the server side. For example, after a user fills out a Contact form you may want to email yourself the details and then redirect the user to a "Success!" page with the following controller code
$this->redirect( '/contact/success' );
Using CakePHP HTML helper is your best bet.
echo $this->Html->link('Link Text Here', 'http://www.anywebsiteyouwant.com);
If it's simple enough, you could just use straight HTML.
What you need is something like:
Router::redirect('/posts/*', 'http://google.com', array('status' => 302));
This would redirect /posts/* to http://google.com with a HTTP status of 302.
See http://book.cakephp.org/2.0/en/development/routing.html

Resources