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.
Related
I have the following routes in Laravel. I wanna be able to call both countries as well as pages on the "root" of the domain, not with any directory prefix.
What I want to achieve is that when no page is found in the eloquent model, that it tries to go open the country and if that fails as well then show a 404.
Is that possible and what do I need to change?
Route::get('/{page}', 'PageController#view')->name('pages.view');
Route::get('/{country}', 'CountryController#view')->name('countries.view');
Edit: I think I was a bit unclear.
The issue is, that the countries.view route is never reached because it fails before with pages.view. Let's say I call /germany - it first matches the pages.view route but no page germany exists. It immediately throws a 404 but I want it to check the country after that and only fail if /germany doesn't exist as a country as well.
How about being agnostic?
one route, you test the parameter, then returning the proper result.
Route::get('/{pageOrCountry}',function($pageOrCountry){
$page = App\Page::find($pageOrCountry);
if($page) return $page;
else $country = App\Country::find($pageOrCountry);
if($country) return $country;
else return redirect('404');
});
There are several ways I will mention two ways:
1- Route Model Binding :
inside page controller create function
public function view(Page $page){
}
this function will return not found if page does not exists
2- Normal check :
public function view($page){
$check= Page::find($page);
if(!$check) abort(404);
}
create resources/views/errors/404.blade.php folder and define 404 error message
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 have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.
I have a Controller witch contains more than 150 functions. almost all of the functions inside the controller are returning views.
each view should change if the user is logged in.
I almost handled this in the view side by using parent blades and etc. But in the controller side for almost every function i should check if the user is logged in and return some specific data about the user along with the view.
for example:
$user=[];
if(Auth::check())
{
$user=Auth::user;
$reputation=$user['reputation'];
$messages=$user->messages();
$notifications=$user->notification();
}
return view('pages.profile')->with(['user'=>$user , 'messages'=>$messages , 'notifications'=>$notifications]);
I thought that this is possible using middlewares but i could not figure it out.
what are the alternatives here?
for more information i am using laravel 5.
One solution could be using your custom function instead of view(). Sth like:
//in your controller action
public function someAction() {
return $this->view('pages.profile');
}
protected function view($template, $data = array()) {
return view($template)->with($data)->with([data that needs to be passed to all views]);
}
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/