Controller method not found Laravel 4 - laravel

I have only this route in routes.php
Route::controller('/', 'HomeController');
and this is the HomeController.php
class HomeController extends \BaseController {
public function getAboutUs()
{
return "About page";
}
public function getIndex()
{
return "Home page";
}
}
the '/' page is OK but when I try to go /about-us page it returns "controller method not found"

You should add a about-us route in your routes.php
Route::get('about-us', 'HomeController#getAboutUs');

Related

Laravel direct to index page having username

I want to redirect to index page having username or other things. Like a branch coordinator when do login , then he redirect to index/{branch_name}.
Please help me to do that.
My Route :
Route::get('/branch' , 'HomeController#index');
Controller :
class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth:branch');
}
public function index()
{
return view('branch.index');
}
}
In web.php change to :
Route::get('/branch/{branch_name}' , 'HomeController#index');
In controller
public function index($branch_name)
{
//$branch_name will be containg the variable
return view('branch.index');
}
To redirect to above page
return redirect()->route('branch/'.$branch_name);

Getting error in Laravel 5.7 edit route page not found

Laravel Version 5.7
PHP 7+
I created a resource controller -> CategoryController [having all the magic methods]
This is the routes/web.php
Route::group(['as'=>'admin.','middleware'=>['auth','admin'],'prefix'=>'admin'], function(){
Route::get('/dashboard','AdminController#dashboard')->name('dashboard');
// product resource controller methods
// check php artisan r:l
Route::resource('product', 'ProductController');
Route::resource('category', 'CategoryController');
Route::resource('profile', 'ProfileController');
Route::post('remove', 'CategoryController#remove')->name('category.remove');
});
Now as you can see, I have "http://127.0.0.1:8000/admin/category/1/edit" for one of my categories to edit with category id = 1, that is also stored in the database.
<?php
namespace App\Http\Controllers;
use App\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function index()
{
$categories = Category::paginate(3);
return view('admin.categories.index',compact('categories'));
}
public function edit(Category $category)
{
return "This is category edit page";
// dd($category);
// $categories = Category::where('id','!=', $category->id)->get();
// // dd($categories);
// return "This is category edit page";
// return view('admin.categories.create',['categories' => $categories, 'category'=>$category]);
}
When I try to go to this edit category page, it shows 404 page not found error.
Although, when I made an individual route for edit method with a closure function to return some text, it worked perfectly.
Route::get('category/{category}/edit', function($category){
return $category;
})->name('category.edit');
You didn't excluded full error you get, but try to change:
public function edit(Category $category)
{
return "This is category edit page";
}
into:
public function edit($category)
{
return "This is category edit page";
}
and see if it helps. If it helps, it means that there is no record matching id you passed or this record is soft deleted (or some additional conditions are not met) - Laravel uses Route model binding to match valid record.
try this
public function edit(Request $category)
{
return "This is category edit page";
}

URL change issue after form post in Codeigniter

how can i manage url in address bar after posting a form or after loading a page after submission.
Is it possible to manage with routing ?
<?php
public function index(){
$this->load->view('login');
}
public function login_process(){
....... code......
if($login==true){
$this->load->view('dashboard'); // Url is not changing but view is loaded
}else{
$this->load->view('login');
}
}
?>
Hope this will help you :
Use redirect() method from url helper , make sure you load it in controller or in autoload.php
public function login_process()
{
....... code......
if($login === TRUE)
{
redirect('controller_name/dashboard','refresh');
/*$this->load->view('dashboard'); */
}
else
{
redirect('controller_name/index','refresh');
/*$this->load->view('login');*/
}
}
For more :https://www.codeigniter.com/user_guide/helpers/url_helper.html#redirect
You should create another method called dashboard and you should redirect to it like following
class Example_Controller extends CI_Controller {
public function index(){
$this->load->view('login');
}
public function dashboard(){
$this->load->view('dashboard');
}
public function login_process(){
// Your Code
redirect('Example_Controller/' . (($login==true) ? 'dashboard' : 'index'));
}
}
replace Example_Controller with your controller name.
and add following lines in routes.php
$route['login'] = 'Example_Controller/index';
$route['dashboard'] = 'Example_Controller/dashboard';

Redirecting to own controller methods

Signup controller:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#success');
}
public function success()
{
echo "successful";
}
}
?>
Routes.php
Route::controller('signup', 'Signup');
So when I go to localhost/signup/new it should throw out successful but ends with Unknown action [Signup#success]. error.
I read many same topics before but didn't help me at this case.
you need to use a certain format.. see RESTful Controllers
it says "Next, just add methods to your controller, prefixed with the HTTP verb they respond to"
change your Signup controller to:
<?php
class Signup extends BaseController
{
public function getNew()
{
return Redirect::action('Signup#getSuccess');
}
public function getSuccess()
{
echo "successful";
}
}
?>
and everything should work now..

NotFoundHttpException for Controller route in Laravel 4

I am trying to get started using controllers in Laravel 4 and I am running into some trouble. Here is the basic run down:
I have a controller in the controllers folder called FansController in the file FansController.php:
<php
class FansController extends BaseController {
public $restful = true
public function getindex() {
return View::make('fans.landing');
}
}
Within my views folder, I have a folder called "fans" that controls a view file "landing.blade.php". It contains simple html: <h1>hello</h1>
In my routes.php file, I have a route calling the controller. Here is that code:
Route::get('landing', array('uses' => 'FansController#index'));
When I visit the url: public/fans/landing
I receive a "NotFoundHttpException";
Do you have any ideas what may be going wrong? Thank you for your help.
First off you need to specify what version of Laravel you are running.
If you are running Laravel 4 then:
// routes.php
Route::get('/', array('uses' => 'GuestController#getIndex'));
// GuestController.php
class GuestController extends BaseController {
public function getIndex() {
return 'Hello world.';
}
}
Then run $ composer dump-autoload -o or php composer.phar dump-autoload -o (if your composer is installed locally) on your CLI
In laravel 3, however
// routes.php
Route::get('/', array('uses' => 'GuestController#index'));
// GuestController.php
class GuestController extends BaseController {
public $restful = true;
public function get_index() {
return 'Hello world.';
}
}
Probably you need to change your method name to index:
class FansController extends BaseController {
public $restful = true
public function index() {
return View::make('fans.landing');
}
}

Resources