In CakePHP have a bunch of unique URL names redirected in routes.php file.
Similar to this:
$beautiful_urls[0] = '/view/location-name/image-name.html';
Router::connect($beautiful_urls[0],
array('controller' => 'Foo','action' => 'bar',3,60));
I want to create facebook like buttons based on the beautified names. In order to do that I need the $beautiful_urls variable I use in the routes.php in the Foo controller.
How can I reach a variable in routes.php from a controller?
So far I tried to link it with App::use('routes','Config'); but it's not working. I also thought about sending the values as action parameters, but that doesn't seem like good practice... I know it's not a great idea to mix the config file with a controller's logic but I don't have any better idea so far.
I'm not cakephp user but simple search shows that there is class called ClassRegistry.
You can create class BeautifulUrls and store it there. According to docs it's singleton and It can be accessed from everywhere.
Also you can make BeautifulUrls implement ArrayAccess interface so you don't have to change your routes
I don't know if it's a good practice or not but my solution was to use the Configure class of CakePHP. It was straightforward to use and accessible everywhere in the code and the config files.
You can save key-value pairs with
Configure::write('key','value');
and read it again with
Configure::read('key');
Related
I'm currently using laravel 5.4 and I have stumbled upon something I can't fix.
I'm currently trying to bind a route to a controller using the Laravel resource helper as such :
Route::resource('campaigns', 'CampaignsController');.
I correctly see my route being there when I do a PHP artisan:route list, I have all my CRUD endpoints tied to the appropriate controller function. Also, note that I'm currently doing that for all my route that need to be tied to a CRUD system ( what I'm working with is mostly form ) without any problem
With this being said, whenever I'm trying to edit a Campaign, I get an error : Class App\Http\Controllers\Ads\Campaigns does not exist
I do not know why it's trying to look for a Campaigns controller while I specify the CampaignsController controller. Everything is behaving correctly in campaigns route, except the edit one. Also, all my other routes have the same logic and never faced this problem.
Any idea why it is looking for the wrong Controller ?
Here's my namespace declaration and folder hierarchy, which is ok ( please note that the adsController has its routes declared the same way and is used the same way too )
here's my edit method
and here's the error
It's quite possible that you try to inject not existing class in your controller.
Take a look at controller constructor or edit route if you don't have something like this:
public function edit(Campaigns $campaigns)
{
}
and make sure you import Campaigns from valid namespace (probably it's not in App\Http\Controllers\Ads namespace.
If it doesn't help try to find in your app directory occurrences of Ads\Campaigns to see where it's used. Sometimes problem can be in completely different part of your application.
EDIT
Also make sure you didn't make any typo. In error you have Campaigns but your model is probably Campaign - is it possible that in one place you have extra s at the end?
Try with Route::resource('campaigns', 'Ads\CampaignsController'); in your web.php file
Being new to Cake on PHP, I am trying to work out if I have a URL, what would be the easiest way to find the controller code for it?
The URL on my local machine is something like:
http://foofoofoo.local/protected/admin/org/edit/1
I have worked out that the location of the view for this file is at this location on my machine:
/var/www/MyApp/protected/app/views/org/admin_edit.ctp
I thought what I'd do is do a search throughout the entire codebase for anything referencing admin_edit.ctp. I found two entries, and changed them to see if I had found the point where the view is called, but despite changing the file name on these entries - the app still works when I visit the URL: http://foofoofoo.local/protected/admin/org/edit/1
I just want to see where the admin_edit.ctp file is being called within the site.
URL: http://foofoofoo.local/protected/admin/org/edit/1
This means I can assume you have a added a route in your /app/Config/routes.php. Where this is pointing can not be said since we don't have access to this file.
Why can I assume you have added this to your routes? Because the posted URL is not matching the CakePHP Conventions which clearly states that controllers should be defined in plural. Since the URL will be accessing the Controller directly through the Controller, unless a route has been specified, I know that the OrgController does not exist. Why?
Try Inflector::pluralize('Org'). It will return 'Orgs' to you. And thus meaning the controller should be called OrgsController and you should be accessing this Controller via the following URL.
http://foofoofoo.local/protected/admin/orgs/edit/1
In this OrgsController there should be an action (function) called admin_edit(), because you have prepended the org with Admin, which is a prefix.
It can be possible that the /protected part, is part of the URL as well, but do not know where your main /App is located and what part of the URL is pointing to the /app/webroot/index.php file.
The Views can be found at /app/View/Orgs/*.ctp.
If you are still having trouble finding your files. Please start with the Blog tutorial written by the Cake Community. This tutorial describes all the neat built-in tricks and will get your first app running in no-time. Please read that first!
If you are still having trouble, feel free to update your question and add the /app/Config/routes.php file.
Under Cake 1.3, if your application has an AppController (check if the file app/app_controller.php exists), you can put this code in the beforeFilter method:
debug($this->params);
It will print an array on your app pages when you are in debug mode, with the name of the controller and the action used.
Array
(
...
[controller] => controller_name
[action] => action_name
...
)
If the AppController does not contain any beforeFilter method, you can just create it:
function beforeFilter()
{
debug($this->params);
}
Let's say I have this in my bootstrap.php file in a cakePHP application:
Configure::write('Config.language', 'eng');
How can I dynamically change the value of that configuration variable based on user action in a controller of my application? I tried to do the same thing that I did above here in my controller, but that didn't work
Any help?
Try Configure::write('Config.language', 'dut'); for e.g.
This answer from the question suggested by #Ryan Pendleton shows a somewhat correct way to use this directive.
It should be used in the AppController because it gets loaded first - as the parent of all other controllers in the application itself.
I used "somewhat correct" because it is best to validate the language codes ('eng', 'fre', 'dut') in the app/config/routes.php file - go here for more information.
Also do check out this: Internationalization-Localization explanation.
I need to create a dynamic url in codeigniter like the facebook application. Is it possible to create such url using the codeigniter framework?
eg:
1. www.facebook.com/nisha
2. www.facebook.com/dev
You need to set up custom routing for the controller in application/config/routes.php. Like:
$route['([a-zA-Z]+)'] = "controller_name/function/$1";
This makes urls like the way you want, but it makes all of your controller inaccessible, that is because any '/controllername/parameter/' format will match with '(:any)' and will be redirected to our 'controller_name/function/'.
To stop controllers redirected by the CI router, you will have to explicitly define all of your controllers on the routes.php first then add the above mentioned routing rule at last line. Thats how i made it to work.
Hope that helps you in some way.
Its pretty easy to setup this by the use of routes. Read their routing guide
$route['([a-zA-Z]+)'] = "controller/user/$1";
However, if their is only one way of accessing the website, is like domain.com/username then its ok, otherwise, this will prove be a hard catch on the long run. On that case, limit the Route to a limited scope like
$route['users/([a-zA-Z]+)'] = "controller/user/$1";
This will help in the extending the system in numerous way
Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.
//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {
$route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
With CodeIgniter I'm trying to create a URL structure that uses a title string as the entire URI; so for example: www.example.com/this-is-a-title-string
I'm pretty confident I need to use the url_title() function in the URL Helper along with the routes.php config folder but I'm stuck bringing it all together.
Where do I define the URI and how is it caught by the routes folder?
Seems to be a straight forward problem but I'm getting stuck creating the URLs end-to-end. What am I missing?
I thought about a catch-all in the routes folder: $route['(.*)'] = "welcome/controller/$1"; ....but how would this work with multiple functions inside a particular controller? ...and maybe it's not even the right way to solve.
You can send all requests to a driver with something like this:
$route['(:any)'] = "welcome/function";
Then use the _remap function to route requests inside the controller.
However, using URL's as you suggest limits the CI functionality. Try something better like www.example.com/article/this-is-a-title-string
$route['article/(:any)'] = "articles/index";
and in article (controller), use _remap...
If you're going to re-route every request, you should extend CI_Router.
The actual implementation depends on what you're doing. If you customize CI_Router, you can do it AFTER the code that checks routes.php, so that you can keep routes.php available for future customization.
If the URI contains the controller, function, and parameters, you can parse it within your extended CI_Router and then continue with the request like normal.
If the URI is arbitrary, then you'll need something (file, db, etc) that maps the URI to the correct controller/function/parameters. Using blog posts as an example, you can search for the URI (aka post-slug in WordPress) in the db and grab the corresponding record. Then forward the request to something like "articles/view/ID".