I'm new to laravel. I'm using laravel 5.8 . I define a new route name calledsignin in routes/web.php and call it in my controller to redirect to this page. but laravel throw an exception with the error "Method Illuminate\Routing\Redirector::signin does not exist
//.../routes/web.php
Route::get('/registration', 'Mycontrollers#index')->name('signin');
//in Mycontroller.php
//some code
public function index(){
//some code
return redirect()->signin();
}
but if I used return redirect()->route('signin'); it works fine
The error you are getting is correct, the Redirector class does not contain a function signin().
If you want to redirect to another route, you have to either use the route name (as in your working example), or the full class with its namespace. For example:
return redirect()->action('Mycontrollers#index');
You could also redirect directly to the path using to():
return redirect()->to('/registration');
Related
I want to use route localization like said here https://codeigniter.com/user_guide/outgoing/localization.html#in-routes
So I need to add route rule like:
$routes->get('{locale}/books', 'App\Books::index');
But I want to make this rule for all controllers - not to specify rules for all controllers. So I added the rule:
$routes->add('{locale}/(:segment)(:any)', '$1::$2');
I have controller Login with method index(). When I go to mydomain.com/Login method index() is loaded successfull. But when I go to mydomain.com/en/Login (as I expect to use my route) I got 404 Error with message - "Controller or its method is not found: \App\Controllers$1::index". But locale is defined and set correctly.
If I change my route to $routes->add('{locale}/(:segment)(:any)', 'Login::$2'); then mydomain.com/en/Login is loaded successfull as I want. But in this way I have to set routes for every controller and I want to set one route to work with all controllers.
Is it possible to set route with setting of dynamic controller name?
I'm not sure it is possible directly, but here is a workaround:
$routes->add('{locale}/(:segment)/(:any)', 'Rerouter::reroute/$1/$2');
Then, your Rerouter classlooks like this:
<?php namespace App\Controllers;
class Rerouter extends BaseController
{
public function reroute($controllerName, ...$data)
{
$className = str_replace('-', '', ucwords($controllerName)); // This changes 'some-controller' into 'SomeController'.
$controller = new $className();
if (0 == count($data))
{
return $controller->index(); // replace with your default method
}
$method = array_shift($data);
return $controller->{$method}(...$data);
}
}
For example, /en/user/show/john-doe will call the controller User::show('john-doe').
If we just create a 404.blade.php page in resources/views/error it will work fine, but Auth() won't work on 404 page, to solve that if we follow the solution available on stackoverflow the Laravel auth errors will stop working. I use the following solution to do the work.
Create custom view
resources/views/errors/404.blade.php
in route.php
Route::any('{catchall}', 'PageController#notfound')->where('catchall', '.*');
create PageController and add this function
public function notfound()
{
return view('errors.404');
}
For Laravel 5.6 and later, you can use fallback in your routes\web.php:
Route::fallback('MyController#show404');
It works as an "catch all"-route.
See docs here.
Write below code in your Exceptions/Handler.php
if($this->isHttpException($exception)){
if(view()->exists('errors.'.$exception->getStatusCode())){
$code = array('status'=>$exception->getStatusCode());
return response()->view('errors.404',compact('code'));
}
}
Now create a new file in your view i.e errors/404;
Now in code array you can pass dynamic values
Not sure if this will help. Laravel has a PHP artisan command to publish error pages. Laravel Custom HTTP Error Pages
After you run this artisan command use Route:fallback() method as #KFoobar suggested. If you use a closure function, no need to use a controller. Make sure to add the below route at the ends of your routes file.
//Fallback/Catchall Route
Route::fallback(function () {
return view('errors.layout');
});
Shameless plug for my BLOG
I have a route setup which is throwing a 404 in my Laravel 5.6 app.
The problematic route is:
Route::get('/project/{project_id}/issue/create', 'IssueController#create');
If I remove the {project_id} parameter the view loads..but I need to be able to pass this id since I will be using it on this view to create new issues that are assigned to a project. All of the other routes work without issue.
My routes file (web.php) looks like this:
Route::get('/projects', 'ProjectController#index');
Route::get('/project/{project_id}', 'ProjectController#show');
Route::get('/project/{project_id}/issue/{issue_id}', 'IssueController#show');
Route::get('/project/{project_id}/issue/create', 'IssueController#create');
And my create function in the IssueController file is this:
public function create()
{
return view('issue.create');
}
You missed project_id as parameter of your create method. Try this:
public function create($project_id)
{
return view('issue.create');
}
and make a route like this:
Route::get('/project/issue/create/{project_id}','IssueController#create');
I am having issues getting the page to render when using a parameter in a create controller. My show controller works but my create controller doesn't. The error thrown is a 404.
Sorry, the page you are looking for could not be found.
The URL is:
http://myapp.test/country/us/state/create
My controller looks like:
// Show
public function show(Country $country, State $state){
return view('state.show', compact('state'));
}
// Create
public function create(Country $country) {
return view('state.create', compact('country'));
}
My route looks like:
Route::get('country/{country}/state/{state}', 'StateController#show');
Route::get('country/{country}/state/create', 'StateController#create');
You need to flip your routes around to be
Route::get('country/{country}/state/create', 'StateController#create');
Route::get('country/{country}/state/{state}', 'StateController#show');
Laravel processes routes in the order that they are defined, so in your current code Laravel was seeing create as a state.
I'm new to FuelPHP and I did a little coding with it! What I did was create a simple controller and created two methods. One for action_index() and the other is action_add().
the code is given below. Views are already in the app\views\ folder.
class Controller_Student extends Controller
{
public function action_index()
{
return Response::forge(View::forge('index'));
}
public function action_add()
{
return Response::forge(View::forge('select'));
}
}
I've set the root to this controller class. When I run the application the index works fine and loads the directed view. But when I give the following URL
http://localhost/project/public/add/
the method doesn't get called! A 404 error is give saying
You can see this page because the URL you are accessing cannot be found.
What Am I doing wrong here. I've gone through every documentation, tutorial I find but I shouldn't get this type of an error. Please help me.
Below is the routing file code :
return array(
'_root_' => 'student', // The default route
'_404_' => 'welcome/404', // The main 404 route
);
You've set the root to student controller, but that doesn't mean all traffic goes through that controller. Try visiting:
http://localhost/project/public/student/add/