I am new to Cake PHP, in regular PHP I had a index.php and for any bad url tried it would just show home. In Cake, I am switching an old PHP and HTML website to use Cake PHP. I already successfully converted one page :(. Which is good but that page is just about_us. I have not done Home (which would be index.php). So here is my scenario
I created all the controllers and models with no code in it except for this one page that gets a bunch of products,but I have the following questions as I do not understand CakePHP documentation:
1) How can I set up an index.php page and where should I put it and Actually this page needs to grab something from the db too. Should it go under views/pages? (i am not sure)
2) Also how do i retrieve parameters in Cake PHP? i used to have index.php?name=blah and just pull name in. I am not too clear on how the cake website says to do it.. you just add the parameter after a /index/2 for example? How come?.
Thank you
I think cake is quite well-documented. You just didn't take enough time to read the book.
1) If you want to create a 'Home' page, it doesn't have to be an 'index.php' file.
(cake has some index.php file to call the bootstrap and dispatch logic and don't mess with them)
You can
- create a view (some .ctp file), put it to /app/View/Pages/ so you can use the url '/pages/' to access the page. Or you can edit /app/Config/route.php file to connect the page to whatever path you want
- create a normal controller/action/view (if you need to grab something from the db so you should have some models, your controller will call the models and pass data to the view). You can edit /app/Config/route.php to connect the page with your path.
Cake is convention-over-configuration. But you should understand both the convention and the configuration. Read the book. Learn more about MVC, about cake's convention and mechanism.
2) You can retrieve the parameter from controller or view (you should do it in the controller) by using $this->param or $this->passedArgs. There are named ones (e.g: profile/name:john) and unnamed ones (e.g: profile/john).
Related
I'm trying to bake my first CakePHP application and am unable to get any page to particularly load for me right now. I've updated my config settings for salt,database, etc. and the index.php page tells me that I have configured everything.
So far I've used cake bake all on just one database table so far to make sure it loads properly. I created the Model, Controller, and View for the standard add/index/view/edit pages. When I try to access URL/organizations/index.php I'm hitting a 404 error however.
Is there any troubleshooting someone might have advice for how to solve this one? It is confusing to me that the index.php loads (so it redirects properly when loading the webroot) but trying to view any View pages yields no results. Is there any debug commands I can do to view what the valid pages would be? Or any additional information I can provide?
If you try URL/index.php/organisations or something similar to this and it works, then there is an issue with URL re-writing on the server which you'll need to correct.
I believe if you have things set up correctly you would want to visit /organizations in order to access the index() method of the organizations controller.
In general the ".php" is left off of the URL, as your index.php file initiates all the bootstrapping and routing. This also requires a correct .htaccess setup. Hard to say exactly what the problem is without seeing the app or an error message.
Try in url
URL/organizations/index.php
To
URL/organizations/index
or
URL/organizations/index.ctp
Cakephp using .ctp extension, that means cakephp Template. Please see this link. And also you can see your app\view\Organizations folder. Here all file is with .ctp extension. Isn't it ?
I am using Dan Wahlin's AngularJS tutorial as a seed project for angular.
The code can be found here:
https://github.com/DanWahlin/CustomerManager
I am having trouble making it function for two different languages.
For example. When the route is : /en/customers or /customers only, I want to show customers.html view which is in /Views/ folder, If the route is /fr/customers I want to load the customers.html ng-view which is in /Views/fr directory.
The problem is routes are registered in app.config. Once the routes are registered how can I change them to work for other languages?
Once the site is loaded and home page is displayed to the user, I have a link which says change language. Once the link is clicked I want all the routes to now have /fr/routeName instead of /routeName. How can I do that.
How am I going to remember the language and reroute all the routes to /fr/routeName instead of just the default /routeName.
I tried using $location.search('lang','fr') to append a querystring value once the user click change language button. But each time a link is clicked the querystring gets vanished. Also even if I have the query string, How am I going to tell Angular that from now on, use /View/fr as the base directory for Views instead of /Views only because the queryString contains lang=fr.
Earlier when I used to make MVVM applications using Kendo's MVVM framework , I used to put fr views in /views/fr folder with same name and english views in /Views directory. Then I checked in Viewmodel that if the querystring contains fr, I compiled fr template from /View/fr folder otherwise I loaded template from /Views folder.
*This is the app.js file which contains app.config:
app.js
*Here is the routeResolver service which has the base directory for views hardcoded:
routeResolver.js
In short:
1) How can I store user's selected language after he clicks a link or button, Is querystring the way to go? Or can I set the default route to point to /fr from a controller for every request from then onwards?
2) How can I tell angular to route to /Views/fr instead of /Views to get the fr View if the queryString contains lang=fr.
I am new to angular and very confused with services,factories,providers etc. Services are not available in config, We have to use Providers, While I dont have Providers in run blocks, instead we have to use Services. Its going to take time before I get a hold of it. I would appreciate any help. You can refer to the git hub project of Dan Wahlin and please tell me how can I change it to respond for other language also. Currently the View directory is hardcoded to var viewsDirectory = '/app/customersApp/views/', in routeResolver.js. How can I make it to /views/fr on user click once the app has loaded and user selects French language as default.
Started learning backbone.js and require.js.
Not sure how to structure files for web app with user authentication.
Seems it should flow like this:
On app init, query server to check auth session state;
Q#1: where should I be writing this 'after init' session code - in /js/app.js?
Q#2: should I be using jQuery ajax for this, or is there better backbone.js methods (I've seen references to get(), fetch(), toJSON() in examples)?
If success, store auth data in a model (user_id, username, auth_token).
Q#3: how/where do I init this model so that I can access that data throughout modules? ie. I will have a view to display template for 'isLoggedIn.html' that will read "Hello %username%! Logout". I want to access 'username' field from this model. Currently, I see only how to create a new model by referencing it in the view's define[], so I don't know how to access the model that was created during init.
Will use jQuery $.cookies to save and get this auth data, so if user leaves page and returns, I can query server to check session instead of requiring user to login again.
Q#4: how do I include jquery.cookies.js plugin into this requirejs app, so that I can later use $.cookies as usual? Am I supposed to add this plugin to the define[] list? Do I have to add it to the /js/jquery/loader.js file?
Thank you for your assistance.
Edit: I used the files from modular-backbone example to create my web app. So when I am talking about /js/app.js and js/router.js, that's the files I refer to.
I'm in the same situation as well.
I found this post and it seems like the best option to do something before every request is to use this solution.
Before accessing and URL except /login, I'm going to authenticate by cookie or run the login view.
About the way to include other folders (jQuery cookie)- just use the require.js mechanism:
In your main file 'require.config' -> 'paths' add the plugin location (jqueryCokkie:)
In your view under 'define' add the path name ('jqueryCookie') and pass it to the finction
The .js file should be in the following structure (I tried to paste the code example here, but got problems...).
I am planning to rewrite my site into CakePHP and after having spent a full week on learning it, I am still not sure how to do good custom routing in CakePHP.
This is what I want:
Keep the current url structure in www.domain.tld/en/dragons.html, or use a www.domain.tld/en/dragons, but not www.domain.tld/en/nodes/dragons.html. And also be able to use controllers on a similar path structure.
There are about 100 static pages on the entire site. I have read into multi-language routing and I think I can do it. I can also make /en/* or /en/:slug route via a PagesControler or a self-written NodesController.
My problem is that I would like to be able to mix and match url's with and without controllers, so actually what I want is that it checks if a :slug is part of the slug-list, there should still be the option to use that url with a controller.
I have created routes for both /en/contact and /en/:slugid, but it seems all queries were routed to my NodesController, even while I explicitly said that /en/contact should be routed to the ContactsController.
How can I instruct Cakephp to keep my current dictorary structure? I read the routes part of the Cakephp book, but it was extremely short and made me a little unsure about the possibility of such routing. If necessary, I'll just write a php-code that prints all routes for all slugs, so I can still write controller-routes with a similar path structure.
If a file exists in webroot (ie. app/webroot/static.html), the .htaccess file will tell Apache to serve that file before loading the CakePHP framework for requests to www.example.com/static.html.
Cake loads routes in a top-down order and will use the first matching route to handle a request. In your case, /en/contact should be above /en/:slugid, else the slugid rule will always win.
If CakePHP's routing does not accomplish what you are after, you can always implement a custom route class (book / example).
I understood (more or less) the separation between the MVC parts in cakePhp, however i cannot understand what are the defaults.
meaning:
What should i edit in order to change the root-entry-point of my site(the known "index.html" or "index.php" file, that shouldn't be changed in cake)?
What controller? What model? What view? What layout?
(hope I'm understood)
(i am using version 1.3)
thanks
What you should edit to modify the root is:
app\views\pages\home.ctp
The default layout can be tweaked here:
app\views\layouts\default.ctp
From there on you can create your menus, links etc to other controllers of other pages, then involving the traditional MVC patterns/conventions you already know.
Addition:
If you want to provide a link to your statistics then use for example:
echo $html->link('My nice statistics',
array('controller' => 'statistics', 'action' => 'show'));
If you would like to embed the statistics then I would use elements: http://book.cakephp.org/view/1081/Elements.
I am not sure about what you missed: maybe the fact you can specify the controller to use for links if it is an external controller to the MVC scope currently used.
Are you sure you have understood the conventions behind MVC? Here is the tutorial I started with some time ago. It is well made but a bit out of date for cakephp 1.3. Nevertheless it illustrates the basic concepts very nicely: Cook web sites fast using CakePHP (IBM)
I hope this is more helpful then :-)
The default Route in Cake routes the address / to the PagesController::display action with the parameter "home", which will make the Pages controller display the file /views/pages/home.ctp. If you just want a static home page, just edit that file.
If your default homepage at / should display more complex data, including model data, you would rather create your own controller with a model and its own directory in /views/ and change the default route for / in /app/config/routes.php to point to an action of that controller.