Laravel 4: Multiple Action in a Single Contnroller Not Working? - laravel-4

I am new to Laravel and am just stuck upon this thing. I have set a controller which contains multiple actions for application. It turns out that if I put a single action in a single controller class, it works quite well. But when I use multiple action. It gives that damn error "Whoops, looks like something went wrong."
So, exactly what am I doing wrong? below is my controller class:
<? php
class ProgController extends BaseController
{
public function showHome()
{
return View::make('home');
}
public function showLogin()
{
return ('you are not authorized to login yet');
}
public function showTravel()
{
return View::make('travel');
}
}
and here is the route.php file content:
Route::get('/', 'ProgController#showHome');
Route::get('/login', 'ProgController#showLogin');
Route::get('/travel', 'ProgController#showTravel');
the files are in their respective default folders. I am worried has it something to do with composer.json file or what? what am I doing wrong?

Turn on debugging in your app/config/app.php file. Make sure you have a home.blade.php or home.php file in app/views (same applies for travel.blade.php or travel.php).
Unless we can see a stack trace that's the best we can give you for now. Definitely turn on debugging for a dev environment.

Use server log, it'll tell you the exact error when you have 500 like this "Whoops, looks like something went wrong.". Most likely you have one component which is not configured right or missing class, missing model, or anything which Laravel can not capture in application level.

Related

CodeIgniter: Undefined property when calling the model

I use CodeIgniter 3 with HMVC.
Does anyone know why do I get the following error
Severity: Notice
Message: Undefined property: Login::$login_model
On the second line below. I clearly call the model, but for some odd reason it is not linked properly
$this->load->model('auth/login_model');
$response = $this->login_model->attempt($username, sha1($password));
Then the model is pretty basic :
<?php
class Login_model extends CI_Model
{
public function attempt($user, $pass)
{
...
Now if I use an object it works, but I have the same issue in many places, including the place where I have
$this->db->select("....
where is crashing as there is no "db". What is the new solution for CodeIgniter 3? I've seen older posts, but none seem to help me out
Thanks!
just try this code put in controller:
public function __construct() {
parent::__construct();
$this->load->model('Login_model'); // load model
}
Problem is resolved, the issues were caused by the fact that my controller extended CI_Controller instead of MX_Controller. So changing
class Login extends CI_Controller
to
class Login extends MX_Controller
resolved the issue.
It took me a while to figure it out by debugging the thirdparty/MX/Loader.php, but once I saw it was looking for MX_Controller type I did the change and it worked perfectly.
This issue is one in many related to migration from CI 2 to CI 3 and also using the HMVC from Wiredesignz. Another big one is uppercase of file names and uppercase on the calls, so strictly referring to this issue I had to uppercase the calls in my controller (changed "login" to "Login"):
$this->load->model('auth/Login_model');
$response = $this->Login_model->attempt($username, sha1($password));
I did the above change already, so this was no longer a blocker, still I wanted to put it here just in case someone hits the exact same issue
Thanks all for your help

laravel routing based on convention

I'm trying to setup a simple routing system based on convention.
My app will have this structure
Http
--Controllers
----Admin
------User.php
----Books
------Add.php
----etc...
I want to be able to add new Folders and controllers without adding routes manually to the web.php file.
For example I want the route to respond to /Admin/User URL with User.php controller.
I'm trying something like this, but I don't understand how to write the internal router...
Route::any('/{module}/{action?}', function($module, $action = 'index') {
Route::get('*',$module.'\'.$action.'#index' );
});
It seems that Rout:get('*'... never matches.
PS the controller namespace is correct and I reloaded with composer.
The controller works if called harcoded.
I tried also to escape '\'
$r=$module.'\\'.$action.'\\'.$action.'Ctl#index';
Route::get('/',$r );
But no result. The route is intercepted but nothing i served
It seems I came up with this
Route::get('/{module}/{action}', function($module,$action) {
return App::make('\App\Http\Controllers\\'
.$module.'\\'.$action)->callAction('index', []);
});
Any other better way?

.net core mvc routing and controllers

So I am brand new to .net. I am learning .net core right now. I am trying to figure out routing. I can not seem to get the routing to look in any folder except Home and Shared. I have looked all over the internet and tried many things. There seems to be something I am missing. Here is what I got
app.UseMvc(routes =>
{
routes.MapRoute(
name: "test",
template: "Register/test",
defaults: new { controller = "Register", action = "test"}
);
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
I have a Register folder with a test.cshtml file in just to try t figure this routing out. And this is is in my HomeController.cs file
public IActionResult test()
{
return View();
}
on my _Layout page I have this link
<li><a asp-area="" asp-controller="Register" asp-action="test">Test</a></li>
It works fine when I put it in the home folder, but I want to keep things separate. I know there is something I am missing. I've poured through all kinds on articles online including Stack Overflow, and I just don't understand what I am missing. From what I read its suppose to be like the Parent folder/File/ then and id that may be attached to that like a user name I have tried other formats for the routing with no luck, this was just my most recent attempt. I just can't help but think I need some bit of code somewhere else.
From your question it looks like you have the following code in your HomeController.
public IActionResult test()
{
return View();
}
That actually belongs in your RegisterController because the route template you defined is an explicit capture with defaults to the "Register" controller and the "test" action.
The view called "test.cshtml" - which should be named as such because of the default convention - should reside in your \Views\Register folder, next door to \Views\Home.
There are a couple of reasons why this may have worked in some fashion. First, the view is discoverable for any controller if it's in shared. Without knowing more about the requests you tried, it's difficult to determine if routing kicked in on the first route or second, but if that method was truly on HomeController requests to /home/test would have worked.
It looks to me like you're exploring routing. That is great - I 100% encourage the experimentation - so long as you know that routing isn't necessarily the lowest hanging fruit to learn. It's also something that you shouldn't have to touch 93.7% of the time. For example, the route you have defined about wouldn't be required for the controller and action you're adding with RegisterController and test.
Cheers.

How can I render a twig template in a custom controller in Silex?

I'm playing with Silex microframework to build a very simple app.
The Silex documentation briefly illustrates how to keep your code organised using controller as class and I've also found this useful article talking about the same practice:
https://igor.io/2012/11/09/scaling-silex.html
but still can't solve my problem
The issue:
in my app.php I'm using
$app->get('/{artist}', 'MyNamespace\\MyController::getAlbum');
This is working. MyController is a class correctly loaded through composer using psr-4.
At the moment the return method of getAlbum($artist) is return $player;
What I'd like to do instead, is returning a twig view from getAlbum, something like:
return $app['twig']->render('player.twig', $player);
To do so, what I've tried to do in my custom class/controller is:
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
[...]
public function getAlbum(Request $request, Application $app, $artist)
but this is generating the following error when I try to access the routed pages:
ReflectionException in ControllerResolver.php line 43:
Class MyNamespace\Request does not exist
Whic made me think that there's a namespace conflict between myNamespace and the Silex namespaces?!
What am I doing wrong?
Is this the right way to make $app visible in my custom controller in order to use return $app['twig']... ?
Thank you in advance!
EDIT
After several other tries still didn't get to the point (replies still welcome!) but I've found a workaround solution that could be useful to anyone will incur in a similar issue. Directly in my app.php I added this
$app->get('/{artist}', function (Silex\Application $app, $artist) use ($app)
{
$object = new MyNamespace\MyController();
$player = $object->getAlbum($artist);
return $app['twig']->render('player.twig',
array(
//passing my custom method return to twig
'player' => $player,));
});
then in my player.twig I added:
{{player | raw}}
And this basically means that I still need an anonymous function to get use of my custom method which is a working solution I'm not really happy with because:
I'm using 2 functions for 1 purpose.
The return value of getAlbum is dependent from the use of "raw" in twig.
SOLVED
The workflow described works fine. It was a distraction error: I've placed the namespace of my custom class after use Symfony\Component\HttpFoundation\Request;
namespace declaration in PHP needs always to be at the top of the file, Silex wasn't able to injects $app and $request for this reason.

Form open to controller method - "Unknown action"

New to Laravel 4. I've created a form within a blade template and I'm following the snippet from which says that you can point a forms action to a controller method by using 'Form::open(array('action' => 'Controller#method'))'. I've created a new controller called UsersController with artisan and have created a new method within the controller named userLogin(). When I point to that method when opening a form I get an "InvalidArgumentException, Unknown action" error. If I adjust the open action to point to UsersController#index, all is well. I've run composer dump-autoload, but the issue remains.
snippet of login.blade.php:
{{ Form::open(array('action' => 'UsersController#userLogin')) }}
snippet of UsersController.php:
public function userLogin()
{
//
}
Can anyone tell me if I'm missing something?
Thanks all. Adding the following to routes.php resolved the issue:
Route::post('login', 'UsersController#userLogin');
Looks like Laravel isn't registering the action you've added, likely because you're missing a route. Try adding something like this to app/routes.php:
Route::post('user/login', 'UsersController#userLogin');
After adding the route to your routes.php, did you also change Form::open()? If not, you can just have your Form post to /login or /user/login.
Also, just because I'm a bit of a stickler for these sort of things, it's common practise to have controllers and models as singular, so UsersController would be UserController, and since the login function is within User(s)Controller, it doesn't need the user prefix. May help your code be more readable :)
Now in laravel 4 you can use this :
Route::post('/signup', array('before' => 'csrf', 'uses' => 'UsersController#userLogin'));

Resources