In a concrete5 website, where is the best place to store global variables that you would like to access site-wide across templates and controllers etc.?
e.g. A specific external URL, a Facebook ID, some kind of version number, etc.
If you want to do this in code (as opposed to via the CMS), you can put this in application/config/app.php:
<?php
return [
'FacebookAppId' => '1234',
];
And then in controllers/templates you can retrieve it via:
<?= Config::get('app.FacebookAppId') ?>
Related
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.
We have some site example.com and I want to make routes for different cities (eg.
example.com/city1, example.com/city2).
We also want to show all other controllers and methods for the current city (eg.
example.com/city1/:controller/:action, example.com/city2/:controller/:action).
This will look like 2 or more different sites that use the same methods in the controllers but will display different info according to the city.
A router prefix won't work for us, because there could be more than 10 cities, and it must use the same methods in the controllers.
We can add cities from the admin panel.
How can we make routes that will take cities from the database and display all the links properly?
Router::connect('/:city/:controller/:action/*', array());
Router::connect('/:city/:controller/*', array());
This will add a prefix to your routes, but all your URLs will be affected. There is probably an easier way too.
Instead of having example.com/users/login, you will need to have example.com/New-York/users/login.
If you want to generate all these links, let us know what kind of controllers and methods you have.
The routes I pasted work for any prefix (city name)
I'd suggest you change your url scheme a little bit, use
example.com/cities/city1 instead of example.com/city1. This change is not only making it easier for routing/rewriting, but it will also rule out the possibility of you adding a city that will accidentally have the same name as a controller!
(Of course there will be some way to keep your original naming scheme, but it will be harder to implement and have the risk of a collision with a controller name)
Once you've got the /cities part in there you can do something like this (sample code taken from CakePHP Routing and not tested, just to give you an idea):
// routes.php
Router::connect(
'/cities/:city/:controller/:action/:id',
array(),
array(
// order matters since this will simply map ":id" to
// $articleId in your action
'pass' => array('id', 'city'),
'id' => '[0-9]+'
)
);
in your controller:
public function view($articleId = null, $city = null) {
// some code here...
}
I come from the server, and in the Laravel framework, we have a concept of 'environments'.
There's a paths.php file, and you can define an array like so:
$environments = [
'local' => ['*.dev', 'http://localhost/*'],
'staging' => ['staging.url.com']
]
Corresponding config files can then be made inside subfolders 'local' and 'staging' and stuff like database settings, etc., is pulled from there.
Why do I want this?
I'd like to auto detect the base URL to use depending on where I am:
angular
.module('myApp.services', [])
.value('homeURL', 'http://local.dev')
//.value('homeURL', 'http://server.com');
Then I can proceed to use {{ homeURL }} in various views of the app etc., Right now, I comment/uncomment the appropriate lines just before pushing to server, but would be nice if there was an easier way...
I found out that I could use angular's $location.host() instead, which fits my needs perfectly.
I have a site that uses CakePHP 2.x. There's a backend interface where actions use the standard Cake layouts and views, but several of the actions are also exposed to front end users as "dialogs" (same functionality, just a layout that can be put in iframe).
In app/Config/router.php I have added the following:
Router::connect('/dialog/:controller', array('action' => 'index'));
Router::connect('/dialog/:controller/:action');
Router::connect('/dialog/:controller/:action/**');
This works appropriately, but the problem starts when trying to use the HTML helper's link() method. If I try to create a link like:
$this->Html->link('edit account', array('controller' => 'users', 'action' => 'edit'));
I get the following:
edit account
When the link is within a dialog, this works great, but I don't want the non-dialog pages to link to the dialog.
How can I control which of the two URLs is used in a particular page?
Is there something I can call from within AppController once I know whether the page being rendered is a dialog or not, or even something in the call to link() that would allow me to override it.
I know there's the "prefix" option which would allow for URLs like /user/dialog_edit but I would like to maintain the /dialog/users/edit format if possible. I also know I can hard code the URL vs. passing controller/action/id/etc in an array, and I don't anticipate pathing/model names changing, but I'd like to do this the idiomatic way for CakePHP, if possible.
I have a cake php application and 4 or 5 pages contain static pages in .php, I have also built some controllers/models and views for those pages that need to be dynamic. My question is.. Can I have a view just with static code there and call it something like
http://myweb.com/app/someemptycontroller/staticview/
Can this be done and if so how do i need to set it up in terms of model and controller..(maybe empty) i am not sure as I am a newbie in this cake php world.
Thank you
CakePHP comes with a working pages controller, which can be used to serve up static pages.
Any view you place in /app/views/pages can be accessed like (say your view is hello.ctp):
http://myweb.com/pages/hello
If you don't like the pages controller showing in the URL, add a line like this to your app/config/routes.php file:
Router::connect('/hello', array('controller' => 'pages', 'action' => 'display','hello'));
Now your page can be accessed by:
http://myweb.com/hello