i have some problem about routes in laravel
Route::get('aset/create', 'TransaksiController#aset_create');
Route::get('aset/{id}', 'TransaksiController#aset_view');
these routes was fine, but when i switch the position like
Route::get('aset/{id}', 'TransaksiController#aset_view');
Route::get('aset/create', 'TransaksiController#aset_create');
the aset/create was went to aset/view
whats happening?
thanks!
The aset/create will trigger the Route::get('aset/{id}', 'TransaksiController#aset_view'); as you can see the template displayed is for view aset-view.blade.php not the template for the create, so laravel identifies create as the id param so your db query will return no rows creating the error you see,
To fix this problem you either keep the original order or you change the view route to match only number(or not match create)
Route::get('aset/{id}', 'TransaksiController#aset_view')->where('id', '[0-9]+');
aset-view.blade.php file, that's where you have issue. You are either accessing a variable as object, it could be something null or array which you are accessing as an object
{id} is a parameter for passing it is receving now create as parameter.since it is in top.Rearrange will solve the problem.id will catch anything you pass
Related
I just want to make a crud api over an "Event" object. Routes for
index works well but the route for an specific event doesn't work
as expected
this is what I have in 'routes.php'
$app->get('/event/:id', \App\Handler\EventRecoverHandler::class, 'event.withId');
I expect to recover the id in the handler using:
$id = $request->getAttribute('id');
but the route only get recognized if I put '/events/:id' literally, in this case the handler is reached but the id is null (as expected)
on the other hand if I put '/events/4' the result is: "Cannot GET http://localhost/event/4"
The problem was that I was following the examples provided in routes.php file, they say that in order to use route parameters you should use /path/:parameter
i don't know what router packages does use this sintax but
in my case I was using FastRoute (the default zend expressive installer selection)
and the right sintax is (following fast route documentation)
/path/{parameter}.
My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)
I am working on a Laravel project, where I want to toggle partials based on the URL data like
foo.bar.com/#itemone/create
foo.bar.com/#itemone/view
Basically, I want to pass whether it is create or view to the partials like this
#include('partials.layouts._core_activity_header',['layout_type' => "create"])
How do I achieve this? Any help would be appreciated.
you can use the route name
Route::getCurrentRoute()->getName()
this will return the route alias
When a user click 'Delete' button, following URI is generated
http://localhost:8888/item?id=32
in my route.php I used
Route::get('item/id={ID}','ItemsController#destroy');
But it doesn't get the input and delete the record. I have created my destroy method properly and when I give URI manually as
http://localhost:8888/item/id=32
it deletes the record.
Why Laravel doesn't capture item?id=32? How to fix this?
Thanks in advance.
You have to change the route to make this("http://localhost:8888/item?id=32") working, you can try the following:
Route::get('item','ItemsController#destroy');
and receive the id in the controller, by doing following things:
$id = Input::get('id');
// then do whatever you want for this id.. here Destroy
Second way
If you want to make "http://localhost:8888/item/id=32" working, then you have to change the delete link. Make the link like the below one:
'http://localhost:8888/item/id=' + {IdWillBePlacedHere}
I got a big search module in my Codeigniter project. Well simply I am passing variable to a view like
<a href=<?php echo site_url('controller/view/1'); ?>>View List</a>
And fetching its data in controller like
$id=$this->uri->segment(3);
For pagination
http://wwww.site.com/controller/view/<filter id>/<page from>
This is working perfectly in the case of simple query.
Now I got some more filter quires like
Country
State
City
Customer type
etc etc
then the url should be
http://wwww.site.com/controller/view/1/id2/id3/i4/id5
Is this the correct way to do the process ? If not please give a little advice...
I am new to codeigniter
The problem you are facing i have recently found a solution for this.
When you are first sending parameters through url use POST instead.
When you get the parameters you can pass them to session in a variable
type. Next time when you paginate get the type value from session and
put it in your query to get the desired result.
If you have more than 1 parameters you can put them in sessions and
unset them on certain conditions so that they are not called in every query.
I think the best approach here is to create another method in the controller something like filtered_view that accepts a filter_id and a page number, and that methode will fetch the data from the database with the provided filter and you'll use your pagination class as usual.
Hope this help.