I have a little routing problem, that i can't solve in Codeigniter 4.
I try adding a parameter at deleting list item. But get the following error messages.
Error message at post routing: Controller or its method is not found:
\App\Controllers\Userfeed::delete
Error message at add or get routing: Controller or its method is not found:
\App\Controllers\Pages::index
controllers file directory:
the relevant part of the Route file (it's in the Config directory):
$routes->get('/', 'Pages/Home::index');
$routes->get('userfeed', 'Pages/UserFeed::index');
$routes->post('userfeed/add', 'Pages/UserFeed::add');
//$routes->add('userfeed/(:any)', 'Pages/UserFeed::delete');//this works fine
$routes->get('userfeed/(:any)', 'Pages/UserFeed::delete');//this works fine
//$routes->get('userfeed/(:any)', 'Pages/UserFeed::delete/$1');//this is not work, which is the goal
//$routes->post('userfeed/(:any)', 'Pages/UserFeed::delete/$1');//this is not work
//$routes->add('userfeed/(:any)', 'Pages/UserFeed::delete/$1');//this is not work
...
the relevant part of the controller:
namespace App\Controllers\Pages;
use App\Controllers\MainCtrl;
...
class UserFeed extends MainCtrl{
....
public function delete($id=FALSE)
{
var_dump('wooot?');
var_dump($id);
}
}
the view part:
....
<a class="badge badge-secondary" href="<?php echo base_url('userfeed/delete/'.$rss['id']);?>" >Töröl</a>
....
But if i make a copy from this controller in the root controller directory, it's working.
$routes->get('userfeed/delete/(:any)', 'UserFeed2::delete/$1');//it's working fine
If need more information let it with me know.
Thanks your help! :)
Note: Thank God, found the problem. :)
And one guess, one reward. =)
Try this one
$routes->get('userfeed/delete/(:any)', 'Pages\UserFeed::delete/$1');
The difference is the slash used. You must use a backslash () not forward (/)
Related
Is there a nice way, to solve this issue: I have a folder ressources/views/project/content with several blade teplates, let's say:
home.blade.php
how-to.blade.php
info.blade.php
best-way-to-score.blade.php
...
Right now, I define one view route per file:
Route::view('/home', 'project.content.home')->name('home');
Route::view('/how-to', 'project.content.how-to')->name('how-to');
...
How can I create these routes on thy fly? I could solve it with a loop through all files in this directory, but maybe there is a more elegant way/function in laravel I don't know yet?
If I understand correctly, what you you need is a generic get route like this:
Route::get('/{page}', 'PageController#show');
and then you need a PageController with a function to return the requested page:
public function show($page)
{
return view('project.content.'.$page);
}
Just have in mind that this kind of route will "catch" every get request so put it at the end of the web.php file
it appears that when I created a new route, I receive the 404 error when trying to access the url, which is funny,. because all of my other routes are working just fine.
My web.php looks like so:
Auth::routes();
Route::post('follow/{user}', 'FollowsController#store');
Route::get('/acasa', 'HomeController#index')->name('acasa');
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Route::get('/profil/{user}/edit', 'ProfilesController#edit')->name('editareprofil');
Route::patch('/profil/{user}', 'ProfilesController#update')->name('updateprofil');
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Route::get('/alerte/url/{user}', 'UrlsController#index')->name('editurl');
Route::post('/alerte/url/{user}', 'UrlsController#store')->name('updateurl');
Route::get('/alerte/url/{del_id}/delete','UrlsController#destroy')->name('deleteurl');
The one that is NOT working when I am visiting http://127.0.0.1:8000/alerte is:
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The controller looks like so:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
class PaginaAlerte extends Controller
{
public function __construct() {
$this->middleware('auth');
}
public function index(User $user)
{
return view('alerte');
}
}
I am banging my head around as I cannot see which is the problem. It is not a live website yet, I am just developing on my Windows 10 pc using WAMP.
Moved my comment to a little bit explained answer.
So, in your route collection, you have two conflicting routes
Route::get('/{user}', 'ProfilesController#index')->name('profil');
and
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
Imagine that Laravel is reading all routings from top to bottom and it stops to reading next one after the first match.
In your case, Laravel is thinking that alerte is a username and going to the ProfilesController#index controller. Then it tries to find a user with alerte username and returning 404 because for now, you don't have a user with this username.
So to fix 404 error and handle /alerte route, you just need to move the corresponding route before /{username} one.
But here is the dilemma that you got now. What if you will have a user with alerte username? In this case, the user can't see his profile page because now alerte is handling by another route.
And I'm suggesting to use a bit more friendly URL structure for your project. Like /user/{username} to handle some actions with users and still use /alerte to handle alert routes.
The following route catches the url /alerte as well
Route::get('/{user}', 'ProfilesController#index')->name('profil');
Since this one is specified before
Route::get('/alerte', 'PaginaAlerte#index')->name('alerte');
The /alerte will go the the ProfilesController instead.
To fix this change the order of the url definitions or change either of the urls to have nesting e.g. /alerte/home or /user/{user}
Well.
Maybe this is too late, but I have all week dealing with this problem.
I made my own custom.php file and add it in the routes path of my Laravel project, and none of the routes were working at all.
This is how I solved it:
You must remember to edit the RouteServiceProvider.php file located in app\Providers path. In the map() function, you must add your .php file. That should work fine!
To avoid unexpected behaviors, map your custom routes first. Some Laravel based systems can "stop" processing routes if no one of the expected routes rules were satisfied. I face that problem, and was driving me crazy!
I would wish suggest to you declare your URL without the "/", like your first "post" route, because sometimes, I have been got this kind of errors (404).
So, my first recomendation is change the declaration of the route. After that, you should test your middleware, try without the construct, and try again.
Good luck!
I am having troubles with connecting one of my routes to its associate controller function.
Routes file
Route::get('/transaction/export','TransactionController#exporter');
Controller and Function
class TransactionController extends Controller
{
public function exporter(){
dd("works");//-->Not seen :(
return view('admin.transactionExport');
}
}
Link in view
Export
When clicking on the link, the address bar in the browser shows the expected url '/transaction/export', but unfortunately it shows me a blank page. It is as though the function in the Routes file does not link to the proper controller. I have over 30 successful links in this site, and have no idea why this is failing on me right now.
Would appreciate the help. Please inform me if more information is needed to solve this.
Change your route to match the controller:
Route::get('/transaction/exporter', 'TransactionController#exporter');
Your previous route wasn't matching 'exporter'.
My project was working just fine till few hours ago. Some routes still working and some stopped. I tried to delete the new modification I made but the problem persists!!!!!
For example, this route used to go to index() method at the guest controller and returns a gust view of activities, and still working fine:
Route::get('activities', 'guestController#showguestactivities');
and this line of code used to go to index() method at the activities controller, but for now it returns just whit-blank page:
Route::resource('admin/activities', 'activitiesController');
I tried to return just a string like that:
Route::get('admin/activities', function(){return 'Just string for Activiteis';});
but it returns the white page again.
I found that there is a conflict happening with this resource:
Route::resource('admin', 'adminController');
once I remove it, every thing else works. But I need this resource and I can't just delete it.
Any help will be appreciated.
It seems that the order is of importance. If you register admin/activities before admin it should work fine:
Route::resource('admin/activities', 'activitiesController');
Route::resource('admin', 'adminController');
I think it should be
Route::get('admin/activities', 'activitiesController#index');
instead of
Route::resource('admin/activities', 'activitiesController');
A newbie here: sorry if the question is too simple, been looking in tutorials but I don't seem to look properly.
I have the
$route['default_controller'] = "mainpage";
in routes.php working properly, and now I just want to view one of the php pages in views folder by typing its url:
http://myserver/folder/thepageIwantToSee
I get a 404 Not Found error. Do I have to use a controller? I just want to see it first before any linking, is there a way to do it?
Thanks a lot!
class yourcontroller extends CI_Controller {
function pageiwanttosee()
{
code
code
$this->load->view('the_view_page', $data);
}
}
Yes, you can write a controller that displays the view specified.
http://myserver/showfile/folder/thepageIwantToSee , where showfile is your controller.
Don't forget to set some security check or disable this on production site.