Lithium PHP routes not working - php

I'm following the quickstart guide for Lithium: http://li3.me/docs/manual/quickstart
I created my posts Model in /var/www/my_app/app/models/Posts.php
<?php
namespace app\models;
class Posts extends \lithium\data\Model {
}
?>
I created my posts Controller in /var/www/my_app/app/controllers/PostsController.php
<?php
namespace app\controllers;
class PostsController extends \lithium\action\Controller {
public function index() {
return array('foo' => 'bar', 'title' => 'Posts');
}
}
?>
And I created my View in /var/www/my_app/app/views/posts/index.html.php
Lithium is less dense than <?=$foo;?>ium.
The quickstart guide then says that I should be able to view my posts index page by going to
http://localhost/my_app/posts
but I get a
Not Found
The requested URL /my_app/posts was not found on this server.
However, if I go to just
http://localhost/my_app
the default home page that comes with Lithium is displayed.
So I tried fixing the problem by adding this line to my /var/www/my_app/config/routes.php file:
Router::connect('/posts', 'Posts::index');
But I get the same Not Found error?

You need to make sure that mod_rewrite is installed and enabled in your Apache.
Also check that the .htaccess file is present and allow_override is properly set for the virtualHost, otherwise .htaccess files will be ignored.
For further information, check the troubleshooting section in the documentation

A little bit further in the Routing documentation, the parameters of the Router::connect() method are explained more fully. The portion after the :: should be the name of the action called by the route; in your case index (or perhaps nothing, if there's a default set for the indexAction; not familiar with Lithium). You derive the "name" by removing the suffix Action from your Controller's method name. I suggest exploring the Routing documentation more fully to save yourself headaches in the future.

Related

Missing Controller when using a prefix admin in CakePHP 3.2

I have an issue with the admin part of my website using CakePHP 3.2.
This part works really well on wamp in local but when I moved the site to the apache server, it stopped working. I have this error message :
Missing Controller Cake\Routing\Exception\MissingControllerException
Error: DashboardController could not be found. Error: Create the class DashboardController below in file: src/Controller/Admin/DashboardController.php
And this error in the variables :
error : Unserializable object - Cake\Routing\Exception\MissingControllerException. Error: Controller class Dashboard could not be found in /data/vhosts/dev.droplet.ninja/htdev/vendor/cakephp/cakephp/src/Routing/Dispatcher.php, line 79
But the Controller exists at the right path with this content :
<?php
namespace App\Controller\Admin;
use App\Controller\AppController;
class DashboardController extends AppController
{
public function index()
{
}
}
The prefix in my routes.php is :
// Admin namespace
Router::prefix('admin', function ($routes) {
$routes->connect('/', ['controller' => 'Dashboard', 'action' => 'index', 'dashboard']);
$routes->fallbacks('DashedRoute');
});
The routes works fine for the public part of the website but not for this. It seems that it can read the prefix and try to go to the file and even ask me to create the exact same file I already have. The only mistery is why it can't find him.
Also the Controller name is in :
src/Controller/Admin/DashboardController.php
I was looking for the differences between the two apaches settings without finding what can make cakePhp have this behavior.
Do you have any idea ?
Thank you
There's a multitude of reasons why it may not work. In my case, it was because of the old routes cache which I had to clear.
bin/cake cache clear _cake_routes_
You can get the list of cache prefixes by running bin/cake cache list_prefixes.
More info: /3.0/en/console-and-shells/cache.html

Laravel 5 Error Pages

I'm having problems creating error pages for my L5 app. I created a controller called BaseController which has all my CSS/JS and every other controller I have extends from it. How does one create an error 404 page which also extends BaseController?
Simply creating a view in views/errors/404.blade.php does not work since no styles are loaded. I'm using Twigbridge which is really useful when working with views.
Am assuming when you say controller has CSS/JS it means that your setting up layout using controllers.
Please note that this works for routes that you clearly define in your routes file and their respective controllers extend the Base.
For Error Pages they are not handle by your controllers. The error handlers are responsible for rendering their views and won't extend any layout. Hence you need to complete define it in your error view.
This should get you off on the right foot. Just use your standard extends syntax to extend your "default" layout.
Do a search for App::error, you will find the right spot.
App::error(function(Exception $exception, $code)
{
$params = array();
$request = Request::create('cms/noroute', 'GET', $params);
return Route::dispatch($request)->getContent();
});
There is no need to handle the errors within a controller or even the assets files. Imagine you have an error from laravel (missing a config file) so you will have the error before event the code could reach a controller. The same case goes for 404. If you do not have that route available, there is no need to let user reach a controller, this is handled by Exception Handler.
A good resource to learn more is Laravel documentation and this blog post

CakePHP plugin throws "Missing View" error but view file exits

I'm writing a basic plugin for my cakePHP 2.x app following the instructions in the book.
I've created the directory/file structure with a MyPluginAppController.php and MyPluginAppModel.php.
I added CakePlugin::load('MyPlugin'); to the parent app's bootstrap.php file.
Then I created one Controller and Model. But for some reason when I try to view mysite.dev/(admin)/my_plugin/my_model/ I get a "Missing View" error. It says to confirm that the view file exists, which it does!
I don't think I skipped any steps from the book. What am I doing wrong?
Update:
Controller path: app/Plugin/MyPlugin/Controller/MyModelController.php
View path: app/Plugin/MyPlugin/View/MyModel/admin_index.php
URL: http://mysite.dev/admin/my_plugin/my_model/
Rename this file
app/Plugin/MyPlugin/View/MyModel/admin_index.php
To this extension .ctp
app/Plugin/MyPlugin/View/MyModel/admin_index.ctp
This is a common mistake.
I was't using a plugin so my case was a bit different. However, this question shows up on the search results page, so I'll post this here.
This is the problematic controller.
<?php
class ArticlesController extends AppController {
public function beforeFilter() {
}
public function view() {
// do stuff
}
}
The problem:
CakePHP kept showing me the following error:
Missing View
Error: The view for ArticlesController::view() was not found.
Confirm you have created the file: Articles/view.ctp in one of the following paths:
Debugging:
I checked all paths it suggested using is_readable() and the stat command to make sure the view file exists and is indeed readable by PHP.
Solution:
The issue there is the empty beforeFilter(). As soon as I removed it everything worked.

How to start a new website project in codeigniter?

I'm really beginner to codeigniter I'm working on CI since last 2 weeks. During this period I have created many views.php files, some controllers.php files and some models.php files
Now I want start a new website project.
What should I do. Should I delete all files of my controllers, views and models, etc., and download another codeigniter and start from the beginning?
You should check the documentation of codeigniter for help but just to give you a quick start ill explain how to create your first codeigniter project.
Installation
1 Download the codeigniter framework from http://ellislab.com/codeigniter
2 upload it in root directory of your website or local apache server directory.
Creating your codeigniter project.
In codeigniter your controller will handle the url requests and load appropriate model and views. So the first step is to create your controller.
1 Creating your controller: go to Applications->controllers and there you will find a built in controller called welcome.php.
This controller loads a view welcome_message.php which is inside Application->views.
You can use this controller or create your own.
To create your own controller create a new php file myfirstcontroller.php and extend a class with same name from CI_Controller.
Note that the name of the file and your class name should be the same. the index function is the default function that will be called when you make a request to the controller
class myfirstcontroller extends CI_Controller {
public function index(){
$this->load->view("myfirstview");
}
}
so when you request this controller through yoursite/index.php/myfirstcontroller
it will load a view called myfirstview.php which will reside inside applications->views.
Go ahead and create this file in applications ->views.
2 To pass data from controller to view you will send an array to the view
class myfirstcontroller extends CI_Controller {
public function index(){
$data['name']="My first application.";
$this->load->view("myfirstview",$data);
}
}
3 You can access this variable in view
echo $name
and it will output your variable
3 you use models you have to create a file inside applications->models and call it from controller and it will return the result in the form of array.
You can look at the documentation for further help.
Hope this helped you to get started with codeigniter.
The user guide is inside your download library.
You can also view it in http://ellislab.com/codeigniter/user-guide/
Good luck!!!
Here is Phil Sturgeon's article on how to do multiple site on one CI instance, in here he explains 2 ways of doing it and describes pros and cons.
http://philsturgeon.co.uk/blog/2009/07/Create-an-Admin-panel-with-CodeIgniter
But in his latest articles he has told what happened to modular separation.
http://philsturgeon.co.uk/blog/2010/03/modular-separation-codeigniter-2

Make redirect in Zend Framework 2 without template

I'm trying to make a redirect from route /admin to /admin/post/list.
I set a route from /admin to IndexController::indexAction()
Then I made controller like this
class IndexController extends AbstractActionController
{
public function indexAction()
{
$this->redirect()->toRoute('postList');
}
}
It works well, but ZF2 required to make a template index/index.phtml.
How I can do this redirect better, without empty templates?
If you add return it should work:
return this->redirect()->toRoute('postList');
I found initially that I couldn't make the return response option work at all, in spite of the docs and plenty of abortive attempts.
In the end I stripped out the default application module that I had included to bootstrap things "out of the box" but wasn't using for anything else, and after shifting the translator config and the factory for it, (as required by the error template - I suppose i could have removed even that, as not required) it started working.
Hey presto!

Categories