I have a plugin named as PanelAdmin. It has Controller UsersController.php and inside it there are different actions defined.
I have called the default controller within the plugin through this code
$routes->connect('/PanelAdmin', ['plugin' => 'PanelAdmin','controller' => 'default','action' => 'index']);
but cannot call other controller if i hit this url:
http://localhost/multi_shopping/PanelAdmin/Users/
One thing more i want to clear is i have to define routes for all controllers actions in routes.php. Please solve my issue. Thanks
In your plugin routes.php make sure you are setting a fallback route.
routes.php
<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
use Cake\Routing\Router;
Router::plugin(
'PanelAdmin',
['path' => '/PanelAdmin'],
function (RouteBuilder $routes) {
$routes->fallbacks(DashedRoute::class);
}
);
From the DashedRoute class:
/**
* This route class will transparently inflect the controller, action and plugin
* routing parameters, so that requesting `/my-plugin/my-controller/my-action`
* is parsed as `['plugin' => 'MyPlugin', 'controller' => 'MyController', 'action' => 'myAction']`
*/
Related
I set up the following routes. The routes are explicit and the controller is in src/Controller directory. I did a git pull on the stage server and suddenly cakePHP (3.6) could not find the method, and was looking in the wrong controller. Below are the routes that are explicitly coded.
Router::scope('/', function (RouteBuilder $routes) {
$routes->connect('/<foo>/methodOne/*', ['controller' => 'SomeController', 'action' => 'methodOne']);
$routes->connect('/<foo>/methodTwo/*', ['controller' => 'SomeController', 'action' => 'methodTwo']);
$routes->fallbacks('DashedRoute');
}
This happens with new controllers I create sometimes. I have added the correct namespaces and use statements. To fix this issue I make changes in the controller and then it works. Any idea why this happens? Anyway to prevent this from happening over and over again?
I have a program that uses $router->resource([]). I use laravel-admin.
here my routes.php
$router->resources([
'programs' => ProgramController::class,
'programs/categories' => ProgramCategoryController::class,
]);
on my programs its work well with all the crud operation.
but on my programs/categories its not working, said not found. did route controller must use different url?...
i mean my category can't be child from my programs with different controller?...
Try changing "programs/categories" to "programs.categories"
You want to add "programs" prefix to categories resource routes. You can do it by changing you code as follows:
$router->resources([
'programs' => ProgramController::class,
]);
// to add programs prefix to categories routes
Route::group(['prefix' => 'programs'], function () use ($router) {
$router->resource('categories', ProgramCategoryController::class);
// here you can add more routes and all those routes will have
// "programs" prefix in there url
});
refer to https://laravel.com/docs/5.5/controllers#resource-controllers
Supplementing Resource Controllers
If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes before your call to Route::resource; otherwise, the routes defined by the resource method may unintentionally take precedence over your supplemental routes:
Route::get('photos/popular', 'PhotoController#method');
Route::resource('photos', 'PhotoController');
So on my case above, just simply change this:
$router->resources([
'programs' => ProgramController::class,
'programs/categories' => ProgramCategoryController::class,
]);
to this :
$router->resources([
'programs/categories' => ProgramCategoryController::class,
'programs' => ProgramController::class,
]);
and it's working well now, also both crud operations.
it's not an optimal solution but its working for me.
I try to learn Laravel, and I'd like to verify the user is logged in before calling a controller to do stuff.
There seems to be at least 3 different ways to accomplish this, and I'd like to know what is the difference between these.
Route::get('/main', 'StuffController#doStuff')->before('auth');
Route::get('/main', array('before' => 'auth', 'uses' => 'StuffController#doStuff'));
Or in the controllers constructor:
public function __construct() {
$this->beforeFilter('auth');
}
There are no differences. Laravel is the Framework that allow you to accomplish many tasks in many ways.
I prefer to add filters in routes grouping them, for example:
// logged users paths
Route::group(
['before' => 'auth'],
function () {
Route::get('/dashboard', [
'as' => 'dashboard',
'uses' => 'DashboardController#mainPage'
]);
}
);
// paths only for admin
Route::group(
['before' => 'admin'],
function () {
Route::get('/admin',
['as' => 'admin_dashboard',
'uses' => 'AdminDashBoardController#mainPage'
]);
Route::get('/categories/{page?}',
['as' => 'categories',
'uses' => 'CategoryController#displayList'
])->where('page', '[1-9]+[0-9]*');
}
);
There is one benefit of such use - its' much easier to look if all routes have correct filters.
Assume you want to display some content only for logged users and you need to use auth filter. You have many controllers to display content for logged users.
If you use beforeFilter directly in those controllers or in parent controllers constructor the following things can happen:
you may forget to put beforeFilter in all your controller constructors
you may forget in your controller constructor to run parent constructor (where you have beforeFilter)
you may extend not the class you wanted (for example you extend BaseController and you have beforeFilter defined in AuthController and in one or some classes you extend BaseController)
Those situations can cause that you display content for unlogged users because you need to remember about auth filter it in each controller and if you want to make sure you did everything right, you need to look at code of all your controllers.
Using route grouping (as I showed above) you can easily look at one file (of course assuming you use one file for routing) and you will see which routes use auth filter and which don't.
Of course I assume many people will have their own opinion on that thing but that's me personal preference to use filters in routes.
Your two ways have no difference, just different syntax style.
I prefer to put the auth filter in a BaseController, then extends all controllers I want to be authed from BaseController. Just write once, used everywhere. Btw, you can also put your csrf filter here.
class BaseController extends Controller {
public function __construct() {
$this->beforeFilter('auth');
$this->beforeFilter('csrf', array('on' => 'post'));
}
}
In my Laravel app I have a resource route where I want to control access to individual routes based on a filter. I do this by declaring the resource with only the "view" routes and then declaring it again with only the "edit/create" routes in a nested filtered group. The filter is a custom one that check the logged-in user's capabilities.
My routes looks like this:
Route::group(['before' => 'auth'], function()
{
$edit_routes = ['create', 'store', 'destroy', 'edit', 'update'];
Route::resource('things', 'ThingsController', ['except' => $edit_routes]);
// We'll filter the routes that involve editing resources
Route::group(['before' => 'edit_resource'], function() use ($edit_routes)
{
Route::resource('things', 'ThingsController', ['only' => $edit_routes]);
});
}
Is this correct? It seems not to work, although no errors are thrown. When I visit a route in the nested filter (e.g. things/create) I just get a blank page.
Is there a better way of achieving this?
Is this correct?
No
Is there a better way of achieving this?
Yes. Here is a great blog post by Phil Sturgeon that explains why you should just define each route manually.
If you really want to continue using the Resource Controller, you can apply the edit_resource inside the controller constructor like this:
Route::resource('things', 'ThingsController', ['before' => 'auth']);
Then in your resource controller
public function __construct()
{
$this->beforeFilter('edit_resource', array('only' => ['create', 'store', 'destroy', 'edit', 'update']);
}
I'm using CakePhp 2.3 and am trying to allow a user to create an account from the home page. I'm new to cakephp and so far I've followed implementation described here: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
Now every time I access the index page (localhost) I am redirected to the users/login page. I've played around with changing the components array in the AppsController, but if I don't include the Auth component then when I call the function add in the USerController, then I get a Call to a member function allow() on a non-object error. I'm not sure how to continue. Right now my AppController class looks like this:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'authorize' => array('Controller') // Added this line
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
All I want to do is be able to add a new user row from the index page. Any ideas or other suggested reading? Thanks!!!
Configure your home page / by using the router to use the posts/index action.
Router::connect('/', array('controller' => 'posts', 'action' => 'index'));
Also I guess you're not calling the parent::beforeFilter() in your whatever controller is used for the home page. Allowing actions in the AppController is a bad idea because it just opens security issues. One day you need an index that should not be public and forget about it OR your have to change all allow() calls.
However, why do you have the login in the posts context? That's wrong. User related actions should belong to the right context: Users controller and model.
If you add the key 'authorize', I think you should define the function 'isAuthorized' as described here : http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
How looks like your default index page ?
If it's the defaut "home.ctp", displayed by the PagesController, you have to allow 'display' as an allowed action :
$this->Auth->allow('index', 'view', 'display');
HTH
go to app->config-> routes.php and change the action to index as #burzum said
and then go to your UsersController or PostsController whatever your controller is call beforeFilter like this
<?php
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
}
public function index(){
}