Cakephp controller name - php

i m new to cakephp. i have stuck in one place the problem is
table name - india_tends
controller - tends
model name - india_tends
The problem is that cake php not allow tends to controller name its show that i have to use india_tends.. so any way to give tends to controller name?

use the variable $useTable="india_tends" and $name="tends" in model

If your table name is india_tends, then your controller code should be in the file india_tends_controller.php, and the class name should be IndiaTendsController.
In your case I think you want the IndiaTendsController to be accessible through /tends/:actions so the best guess would be using routing.
Edit your config/routes.php to have the following code:
Router::connect('/tends/*', array('controller' => 'india_tends'));
And you can have it accessible through /tends/*

Which version of CakePHP are you using?
You could use routing.

Related

Laravel Duplicate Route names

I'm using same route name for the get & post methods in route. those routes are using for the same purpose.
ex : I'm calling to load add view form using get route
Route::get('add', 'UserController#addView')->name('user.add');
then,
I'm calling to store data in that form using post route
Route::post('add', 'UserController#store')->name('user.add');
is there any issue , If I use the same route name like this?
No, you can not use the same name for 2 different routes as is stated in the documentation if you really need to name the routes you should look for different names, but if there's no need to have named routes you can have each url with its method like:
Route::get('/your-url', 'App\Http\Controllers\UserController#addView');
Route::post('/your-url', 'App\Http\Controllers\UserController#store');
If you are making a CRUD you can have:
Route::resource('user', UserController::class);
This will create all the urls needed for a CRUD:
Actually you have same name for both routes i.e., name('user.add'). Change that.
Yes you can get issues in future. For example, I had issues after functionality update - I've updated route interface (added some fields) and new fields does not appeared in result after opening URL.
Please run command php artisan optimize to see is it all ok.

Wrong controller is being used for edit route ( using Laravel resource helper )

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

is there any way to remove or hide the controller name in url? codeigniter

I developed a project in php codeigniter. The project is almost complete but now my manager wants me to completely remove or hide the controller names in the URL. My current URL looks like this:
http://www.sitename.com/Controller_Name/function_name
and my manager wants it to look like this:
http://www.sitename.com/function_name
Note that I have more than 10 controllers in my project and many many functions. I want a method that applies to all. HELP PLEASE.
You can do this using $route.
If you have only one Controller and in that controller you all the functions you can write something like this:
$route['(:any)'] = "Controller_Name/$1";
If you have many Controllers you need to specify for each function to what controller to point. Like this:
$route['function1'] = "Controller_Name1/function1";
$route['function2'] = "Controller_Name1/function2";
$route['function3'] = "Controller_Name3/function3";
And you can't have duplicates in $route
This $route array should be located here: application/config/routes.php.
You can check more about routes on the CI documentation here: https://www.codeigniter.com/userguide3/general/routing.html
You have to specify your controller and method name in routes file like below
$route['function_name'] = 'controller_name/function_name';
You have to write this for each function in controller.By doing you get two advantages
1) Controller name will be hidden in your url
2) you can call some method by its name only.No need to use controller name every time.

Can you add a prefix to controller file names in Codeigniter?

I like to name my files with a prefix so that when I have them open in an editor I can distinguish what kind of file I'm working with easily.
So instead of naming the controller file home.php or account.php, I want to be able to add a prefix to the file name like controller.home.php.
Is there a configuration option in Codeigniter that let's you do this?
No, there isn't such an option, there's no practical reason for it to exist.
Controllers typically extend the CI_Controller class, so that helps you know what type of class you're editing, if that is of any use to you.
There is no configuration in code-egniter, i guess.
You can not use "." in name, instead of this you can use naming convention like
"controller_home.php" or "controller-home.php"
"controller_account.php"
May be this will help.
in this case, you need rewrite CI router according to your logic

How to use variable from routes.php in controller

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');

Categories