I'm using FuelPHP for my website. Currently, I've the following structure:
-folder1
-FILEHERE.php
-folder2
...
-fuel
-app
-classes
-controller
-learn.php
-folder1.php
-public
I've created a controller in fuel->classes->controller with the name learn.php. Now what I want is this:
If the person visits http://example.com/learn -> this controller fires up (learn.php).
I want to add a sub-controller and throw control to that whenever there is a next directory request. i.e.:
http://example.com/learn/folder1 should throw control to folder1.php in fuel->classes->controller.
How do I do this? I've checked the methods action_* but they don't look flexible.
Also, once the user visits something like: http://example.com/learn/folder1/FILEHERE
It should load the content file from folder1->FILEHERE.php (see the directory layout above)
Thank you for your help!
You can easily do this using FuelPHP's routing functionality. Which is documented here.
Your basic route config for the folder1 controller might look something like this:
'learn/folder1' => 'folder1/index',
The documentation outlines more complex examples as well if needed.
Related
When I make new component I would like to have base component like Modal within root directory as it is made:
app/View/Components/Modal.php
And then I would like to have something like
app/View/Components/Modals/Login.php
app/View/Components/Modals/Register.php
app/View/Components/Modals/ResetPassword.php
How could I use/call these components like ones from root folder? (Components as root folder)
<x-modal></x-modal> - Works fine
But <x-login></x-login> or <x-modals-login></x-modals-login> doesn't work. Is there something that can work like this?
The documentation explains this:
If the component class is nested deeper within the App\View\Components directory, you may use the . character to indicate directory nesting. For example, if we assume a component is located at App\View\Components\Inputs\Button.php, we may render it like so:
<x-inputs.button/>
So in your case, this would be:
<x-modals.login>
So basically my site was pre-made so that pages did not exist. The only features that existed was a directory and some posts. No pages. So I had to duplicate the posts function (meaning I replaced all instances of $post and $posts with $page and $pages). It all worked fine. I created a test page which worked. But now I can't access my backend.
This is the URL structure of the pages as seen in the routes file:
Route::get('{url}', 'postsController#viewpage');
So it will basically look like this: www.mywebsite.com/pagehere
But the backend's URL structure looks like this: www.mywebsite.com/admin
So I wonder if my page structure is conflicting with the admin backend?
Whenever I try to access the backend, I get redirected to the homepage.
My controller file has this:
public function adminpage(){
return view ('admin/index')->with("title","Admin");
}
Seems like the rule you put in place matches every route:
Route::get('{url}', 'postsController#viewpage');
You need to specify that the given url must exclude routes tarting with admin, via a regex you can achieve this:
Route::get('{url}', 'postsController#viewpage')->where('url', '[^admin]');
Also I would advise you to use appropriate naming convations: PostsController
Laravel route regex docs
I would like to add a new phtml file to my index folder in which I already have several views:
index
landing
And so on... I access them by using the following logic:
sitename.com/index/landing
or
sitename.com/index/index
How can I add the phtml file (my new view) to my index folder so that I'm able to see it when I enter in the browser:
sitename.com/index/mynewview
I'm quite new to the whole Zend Framework, and I'm not sure how the structure works exactly, so I'd like to find out more. Can you guys help me out with this, how am I supposed to do this?
Thanks heaps! :)
P.S. The views are in the following directory structure:
module/application/view/application/index/
and then here are all of the views, this is where I'd like to add my new view and access it from browser like this:
/index/testview
Edit:
When I add the testview.phtml to the index directory and put some test tags like this in it:
<h1> Testing new view page </h1>
It's not being rendered on the browser
Because this is an MVC framework, you're skipping a few steps. You're probably going to get a few harsh responses, but I'll try to fill in the holes for you very quickly.
Ignore the file folder structure for a minute.
This is a route:
/index/landing
Routes point to Actions inside of Controllers to work.
Assuming you have started with the skeleton, open up your module's module.config.php, you should see route config, e.g.:
https://github.com/zendframework/ZendSkeletonApplication/blob/master/module/Application/config/module.config.php#L29
You'll need to add a config entry for the routes you want to serve. It could be as simple as a Literal entry for /index/landing, or something more complex (Segments, Regex, etc.) that handle patterns for routing. Spend some time tinkering and learning here; routes are pretty critical to working with MVC.
When configuring the route, the assumption is that you have a Controller set up, and that this Controller has an Action (to which your route is pointed). That Action, is where you can connect template files (phtml,twig,etc.) to routes:
// dummy action that serves index/testview
public function fooAction(){
$vm = new Zend\View\Model\ViewModel();
$vm->setTemplate('index/testview');
return $vm;
}
That index/testview, will be in your module's view templates, not in your public folder.
I think that's a reasonable primer to get you on your way!
Take some time to learn:
http://zf2.readthedocs.io/en/latest/index.html#userguide
Maybe start here:
http://zf2.readthedocs.io/en/latest/in-depth-guide/understanding-routing.html
ZF2 (V3 is coming!) is a beautiful thing, it's worth it.
Good luck.
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);
}
in my current project ive a controller tree like this:
Controller -
------Admin -
------------user.php
------otherClass.php
Where controller and admin are folder and user and otherClass are the classes.
If i want to call any otherClass method the url would be this one:
example.com/otherClass
But when i try to call the users methods like this:
example.com/admin/user
i get this : Class controller_admin does not exist (whats logical), so i tried with the routs in the bootstrap.php and after many failed tries, i gave up and decided to ask you guys :P .
The question is how should i code the route::set to make this work.
Thanks
First thing is that you must set 'index_file' key to FALSE in bootstrap (when Kohana::init() is being called).
Leave the default route as it is ( it's controller/action/id, with controller and action being 'index' by default).
For more informations take a look at kohana 3 routing basics article