Using cake 2.1.1. I'm trying to make a cron job to execute an action from a controller. Which is right way to do this? I have the OffersController with an action called admin_test. I'd wish to run this action every 2 hours. For the moment I made a shell in app/Console/command/SyncapiShell.php:
class SyncapiShell extends AppShell {
public $uses = array('Offer');
public function main() {
$this->Offer->admin_test();
}
}
But I get a SQLSTATE[42000] Syntax error or access violation trying to execute the shell.
I'm also using the admin routing, the auth component and ACL. How does the shell work? It ignores the authentication and the acl rights? Normally the admin_test action may be accessed only by specific authenticated users.
Thank you
No this is not the right way to do but more likely a strong indicator that your app architecture is not good. You should have fat models and tiny controllers.
Shell:uses will work like the uses property of a controller and load models. In Controllers you should use the model associations and not load thousands of models using uses.
Refactor your controller method and move the code into the Offer model.
And why does a shell need authentication or admin routing? A shell is, as the name says, a shell program, NOT a website. Authentication is basically done by the OS and user who runs the script. Only people who have access to the shell will have be able to run it anyways.
Related
Im building an app with AngularJS and Laravel. I'm currently working on a login page.
By default in Laravel, the 'out of the box' class User extends 'Authenticatable' but I would like to know if I can rather simply extend it to 'Model', because I actually want to rule my authentication access through AngularJS routing and not via Laravel. I don't know if this makes sense.. and if this would even work (I dont wanna break the logic behind Laravel). Could anyone help me on that?
The Authenticatable refers to Illuminate\Foundation\Auth\User which in turn includes some Traits and implements some contracts, depending on whether you're going to use Laravel's Auth system it seems like you can omit extending this class without any problems.
I've been working on cakephp3 for a while now. I've always used Cakephp's Auth component for authorization and authentication purpose.
I follow the very conventional procedure every time, like loading the component, adding isAuthorized function in controllers and defining allowMethods etc.
But now what I want is to develop my own plugin for this purpose, just using Cake's Auth component. So that i can reuse the plugin in all my future projects, also i want it to be like plug and play. Like You enable it, add few settings and your User management is done.
I know that how migrations work so I can add users table via migration every time. (Just an idea)
The thing I don't get right now is how to make everything separate from the core app? Like everything is done via plugin and nothing is added to every controller of the app.
Hope I'm clear about what I want to achieve.
Update: I know there is a whole list of third party Auth plugins. But I want to develop my own so i just need the idea of how things work.
Any solutions to my problem would save my day.
I'm sure there is a relatively simple answer. I am trying to use CakePHP 2.0 to bake an application from the command line, and i would like cake to create the basic AuthComponent methods for my user model. the database table for users is named users, which includes the two necessary fields for auth, username and password.
I've been able to include AuthComponent in my usersController from bake, but have been unsuccessful at getting it to generate basic usersController actions for auth, such as login() and logout().
How can I do this? generating this skeleton code will help save a lot of time.
Thanks in advance!!!!
default bake templates are only for CRUD (create=add, read=index,view update=edit, delete=delete), not for the rest. but it is fairly simple to customize your templates according to your needs.
Although login/logout are one-time-needed methods and don't make any sense to be baked. Its easier and faster to just manually "make" them. For login use the add template as basis, logout doesnt need any view.
I´m just working with Cakephp for a few days and I´m very impressed. But now I´m trying to get closer with Acl, but it´s a bit confusing.
My situation is, that I want to create a website with a frontend and a backend. But I´m not sure if I really need Acl for this, cause all Pages should be available for all users, except the backend of course. The tutorials in the Cookbook aren´t very helpful due to the fact, that it´s all about creating users, and groups and roles and creating the right views for login, adding and editing users, etc.
But I just need information about what Acl handles? Does it restrict the use of controllers or models?
Or do I need something else than Acl? Maybe it´s easier to check a session variable and redirect direct into the controller if the check false?
Hopefully you can bring me on the right way,
thanks in advance and best greetings from Germany,
Sascha
I suggest you to read this chapter and use the Auth component instead of simply accessing the session as you're teased to do.
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
For your admin backend use prefix routing.
http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
In conjunction with auth this is pretty easy to check and implement in the isAuthorized() callback.
If you don't need various 'levels' of permissions; i.e. any logged-in user is allowed to access the backend, it's best to skip ACL (for now). If, in a later stage, ACL is required, you can always add it later.
You can start with 'simple' authentication. This chapter in the cookbook describes how to do so;
http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html
In general, do not develop features that you don't need now. E.g. implementing ACL because you might need it in the future is only overcomplicating your development and chances are, they don't fit the requirements when that moment arrives.
Unlike #burzum, I'm not a big fan of prefix routing (only for very simple projects), because you'll end up mixing front- and backend actions and logic in the same controller.
I would advice to create separate controllers for the backend, possibly develop them as a Plugin. Either way, you may consider to create 'base' Controllers and Models for the backend and have your backendcontrollers/models extend them. This way you'll be able to define components/behaviors to use for the backend in 1 location. Also, by loading the 'Auth' component only in Backend controllers, you don't have to 'allow' actions in each controller in the frontend
for example;
class BackendCoreController extends AppController {
// only load the Auth component in backend controllers
// regular/frontend controllers don't require authentication
$components = array('Auth');
}
class PageAdminController extends BackendCoreController {
}
For considerations on developing the backend as a plugin, see my answer here:
Best way to implement admin panel in CakePHP
Update: i wrote some wrong statements about the use of header in php; so forget that part :)
What i want is to fetch and display a controller's view (with controller's data) from another controller, without have url change in browser.
Some details:
Redirect doesn't do the job because is a direct redirect (via browser)
requestAction doesn't allow me to fetch css and images correctly
I need this thing because i have a controller dispatcher that redirects internally to the other controllers.
I think the only (correct) solution is to use routes.php in /config with Router::connect
and there use the logic that was in the dispatcher controller.
ummm... header() is the function to use for a redirect unless the PHP documentation is wrong. (http://php.net/manual/en/function.header.php) The core in cakePHP uses header for the redirect function (see lines 721 - 730 of cake/libs/controller.php).
So I am not certain what you mean "like normal PHP". CakePHP is PHP, it's just built on object oriented code. It's not magic or twisted ways of doing things. So to do a redirect in cake, you can simply use:
$this->redirect(array('controller' => 'my_controller', 'action' => 'my_action'));
And it will call the header() function.
Now. If you are dead set on not using redirect (maybe if you are going to an external site), you can call header() in the code. Just be sure you put the exit(); after the header call:
header('Location: http://call/my/url');
exit();
It will work just the same as redirect. It's just a lot of unnecessary extra work. Keep in mind that using redirect will maintain the domain name and build the URL for you automatically.
In general, connecting URLs to controllers is the job of routes. If your logic is rather complex and normal routes won't cut it, you can even write your own route parser class that does more complex logic (that's all in the manual).
If this routing logic involves database queries or any other sort of controller logic and may lead to very different output for the same URL based on some internal state though, you're making a very RESTless application and I'd submit you should rethink what you're trying to do. Having said that, you can render any view from any controller action using $this->render(). The controller logic for each view could be put in the AppController or possibly (partly) the models to be called from anywhere. So instead of "redirecting" to a different controller, a route just routes to a specific controller action as usual, that action dynamically calls code it needs to call and then renders the view it needs to render.
If you want your app to stay on the same URL but display very different content, you should probably also look into making an AJAX app.
The right solution for you is probably somewhere in between.