CakePHP - How to get public path to application root - cakephp

I'm looking for a constant or variable that will provide a public path to my application root.
I have got so far as FULL_BASE_URL which gives me http://www.example.com but I have the added problem of my application being in a sub directory (e.g. http://www.example.com/myapp/).
Is there any way to get the path like http://www.example.com/myapp/ in my controller?

$this->Html->url( '/', true );
In general you should generate all links with that function, see http://book.cakephp.org/view/1448/url

$this->base;
http://api.cakephp.org/class/dispatcher

<?php
...
$this->redirect( Router::url( "/", true ));
...
?>
Router is the static class used by the HtmlHelper::link, Controller::redirect etc. the Router::url method takes a string, or array and matches it to a route. Then it returns the url that matched the route info as a string.
If you pass "/" to the Router::url call you get a relative link to the root of your app. If you pass "/" and true to the Router::url call you will prepend the full BASE_URL to the resulting relative path. This should give you what you need. If not, here is the link to the Router documentation. Try experimenting with the second boolean param - it may or may not work as expected depending on what you read / your own testing.
http://api.cakephp.org/class/router#method-Routerurl

Related

NextJS and next-routes not routing as I would expect

I've created a routes.js file that is as follows:
const nextRoutes = require('next-routes');
const routes = module.exports = nextRoutes();
routes.add
('presenterallcurrent', '/presenter/:ccyear','speaker');
routes.add
('speakersessiondetail', '/presenter/:ccyear/:slugSpeaker','speakerdetail');
I am expecting that when I browse to:
/presenter/2018/douglas-crockford-1124
I will be taken to my /pages/speakerdetail.js file with query.ccyear and query.slugSpeaker populated to 2018 and douglas-crockford-1124
I am expecting that when I browse to
/presenter/2018
I will be taken to /pages/speaker.js with query.ccyear set to 2018
The second case works as I expect (/presenter/2018) does take me to /pages/speaker.js, but (/presenter/2018/douglas-crockford-1124 gives me a 404.
What am I not understanding and why does this not work?
In my /pages/speaker and /pages/speakerdetail:
static async getInitialProps({query}) {...}
For optional parameters in your route, add a trailing ? and adjust speaker.js to render either the year view or detail view based on whether the query.slugSpeaker is undefined.
routes.add
('speaker', '/presenter/:ccyear/:slugSpeaker?');

UrlMatcher matches route with parameter when parameter does not exist (not like in the documentation)

I have two very simple routes (among others):
.state("master", {
abstract: true,
url: "/{tenantName}",
...
})
.state("default", {
url: "",
...
})
This is from the documentation:
'/user/:id' - Matches '/user/bob' or '/user/1234!!!' or even '/user/'
but not '/user' or '/user/bob/details'. The second path segment will
be captured as the parameter 'id'.
So now if I go to www.myserver.com I exect the second route to kick in, because according to documentation it should not match the direct url, but in fact it does and causes me lots of troubles. Am I doing something wrong?
P.S. I want www.myserver.com go to second route and www.myserver.com/tenant1 to go to first route.

Cakephp 3 : Localization Routing

I add route like below
Router::connect('/:language/:controller/:action/*',
array(),
array('language' => '[a-z]{3}'));
I have two language src/Locale/jp and onother is src/Locale/fr
After add route configuration I have tried below URL
project/jp/tests/index
It's giving me error JpController not found.
How can I configure route for localization in cakephp 3.
Update :
In before filter I have added below code , but language is not changing
if($this->request->params['language']=='jp'){
Configure::write('Config.language','jp');
}
Have a close look at what you are passing, jp, that's two chars, now look at your regex, it requires exactly {3} chars - consequently, the route will not match.
On a side note, the folder name should be Locale, not local.

CakePHP allow forward slash as named parameter value

I am using cakePHP and got stuck in a situation. I want to pass a named parameter to my action that contains forward slashes. For example
http://www.test.com/claim/index/user:rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
Basically "rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=" is one parameter but cake is only recognizing the first part of it until the fist slash.
When printing the named params array I get:
Array
(
[user] => rLbu78h2RpVwLTy
)
How can I escape the forward slash and allow cake to accept it as part of the named parameter?
Thank you
Don't use named-parameters
While you can use named parameters (/foo/this-is:a-named-parameter/), it's best to stick to normal url positional/path arguments and/or GET arguments. Over time it has proven not to be a great solution to pass around information using named parameters.
Use a trailing star route
If you modify the format of the url you're using to be e.g.:
/claim/index/<user token>
Then you can use a trailing star route to captuer everythiing that occurs after /index/ in a single variable:
Router::connect(
'/claim/index/**',
array('controller' => 'claims', 'action' => 'index')
);
In this way it doesn't matter what comes after /index/ you'll receive it in your index action:
// Request to /index/rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4=
function index($user) {
// $user === 'rLbu78h2RpVwLTy/bki3pK1NkkXhCCaYfQ/zXDIfZR4='
}

how to set default prefix in controller -> skip defining prefix in links

I have the following idea: I'd like to be able to define the default prefix in any given controller. So let's say the default prefix for the CitiesController implements all actions with the "admin" prefix ("admin_index", "admin_add", etc.), but the ProvincesController implements all actions with the
"superadmin" prefix ("superadmin_index", "superadmin_add", etc.)
The problem with this is, every time I want to link to any "city stuff", I have to specify "admin" => "true". Any time I want to link to any "province stuff", I have to specify
"superadmin" => "true".
That's already quite a bit of work initially, but if I decided I wanted to change the prefix from "admin" to "superadmin" for cities, it would be even more work.
So I was wondering if there's to somehow do something along the lines of:
class CitiesController extends AppController {
var $defaultPrefix = "admin"
}
And then in the HTML helper link function, do something like:
class LinkHelper extends AppHelper {
public $helpers = array('Html');
function myDynamicPrefixLink($title, $options) {
// check whether prefix was set (custom function that checks all known prefixes)
if (! isPrefixSet($options)) {
// no clue how to get the controller here
$controller = functionToGetControllerByName($options['controller']);
// check whether controller has a defined default prefix
$prefix = $controller->defaultPrefix;
if ($prefix) {
// set the given prefix to true
$options[$prefix] = true;
}
// use HTML helper to get link
return $this->Html->link($title, $options);
}
}
I just have no clue how to get from the helper to the controller of the given name dynamically.
Another option would be to store the default prefix somewhere else, but for now I feel that the best place for this would be in any given controller itself.
Another idea would be to even have that look up function dependent on both, the controller and the action, and not just the controller.
You should be able to use the Router::connect to supply defaults (see code and documentation on Github: link) to specify default prefixes for certain controllers and even actions.
However, if you want more flexibility than the current Router provides, you can extend your use of the Router::connect by specifying an alternate Route class to use:
Router::connect(
'/path/to/route',
array('prefix' => 'superadmin'),
array('routeClass' => 'MyCustomRouter')
);
Examples of this can be seen in the CakePHP documentation.

Resources